Can someone explain this line [duplicate] - java

This question already has answers here:
Difference between moving an Iterator forward with a for statement and a while statement
(5 answers)
Closed 5 years ago.
hello i'm trying ton understand a programm and a line giving me some trouble. Can you explain me the Construction of this line because i have never seen a "For" like that
for (Iterator<IBaseVO> itMachine = machinesSelected.iterator(); itMachine.hasNext();) {
MachineVO mach = (MachineVO) itMachine.next();
idsMachines.add(mach.getMchId());
}
thanks you

A for loop can have 3 arguments, but it's not required.
Usually goes like this
instruction
boolean
instruction
the first and the third one can be omitted safely.
So here is the first instruction slot used to initialize the iterator object.
The second instruction slot is used to see if there are new items in the iterator.
the third slot is not needed and thusly omitted.
This does the same as
Iterator<IBaseVO> itMachine = machinesSelected.iterator();
while (itMachine.hasNext()) {
MachineVO mach = (MachineVO) itMachine.next();
idsMachines.add(mach.getMchId());
}

Related

pass array as argument without first element java [duplicate]

This question already has answers here:
How to use subList()
(6 answers)
Closed 3 years ago.
I want to pass an array as argument to a function without first element.
I came up with this solution, but I'm wondering if there are better ways to it.
List<Integer> numbers = new ArrayList<>();
//... in the meantime numbers is an array that contain 1000 elements;
numbers.remove(0);
myFunction(numbers)
You can use subList(firstElement, lastElement); method.
Here please check javadoc.

Print current line number in intellij [duplicate]

This question already has answers here:
How can we print line numbers to the log in java
(22 answers)
Closed 3 years ago.
I am trying to print the current line number(line number of ide) of a certain code from intellij. How do I do that?
1 System.out.print(getCurrentLineNo());//sample code
2 System.out.print(getCurrentLineNo());//sample code
3 System.out.print(getCurrentLineNo());//sample code
4 System.out.print(getCurrentLineNo());//sample code
One way to do this is to get the stack trace element and call getLineNumber.
public static int getCurrentLineNumber() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}
Note that I used an index of 2 because that refers to the frame of the method that calls getCurrentLineNumber. If you are just doing this inline, i.e. like this:
System.out.println(Thread.currentThread().getStackTrace()[1].getLineNumber());
You should use an index of 1 instead.
Note that an index of 0 refers to the frame of the getStackTrace method.
I suppose it too expensive to get StatckTrace for that, but it can help you for some debug.
System.out.println(new Exception().getStackTrace()[0].getLineNumber());
Also, you can look here
You can get line number like this...
public static int getCurrentLineNo() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}

How to access coursor/index of for each loop [duplicate]

This question already has answers here:
Java, How do I get current index/key in "for each" loop [duplicate]
(6 answers)
Closed 7 years ago.
I've been wondering if it is somehow possible to get the current index of a for each loop in Java without a seperate count variable.
In debug mode I can see the loop in the following structure:
i$ = {ArrayList$Itr#17838}
cursor = 1
lastRet = 0
expectedModCount = 5
this$0 = {ArrayList#17836}
So I guess cursor is the index. Is there a way to access the cursor?
Thanks for your help
So far
Not to my knowledge. But even if there were a way, it would be counterproductive to access. The reason why one would want a for:each loop is because it is quick, and cycles through a whole array in a simple manor. If you want to have a cursor index to access, use a for loop and just access the incrementing variable.

Java substring issue [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
EDIT: Thank you, PakkuDon
instead of using "==" I must use ".Equals()"!
I'm trying to implement a chat-command system though I've encountered troubles in differentiating between standard chat and command chat..
Focusing on just one command for now, `highlight
the output whenever I type in `highlight is:
highlight
`highlight
Here's my code:
String cmd = InMessage.message.substring(0, 10);
System.out.println(cmd);
System.out.println("`highlight");
if( cmd == "`highlight" )
{
... cancel chat packet and proces command
}
and yet the if statement returns false.
What's going on here? Anything I've done is wrong?
You have a number of problems. Firstly, that substring is going to throw an exception if the user types something that's less than 10 characters. Secondly, you're using == where equals would work better. But you don't need either of these things. You could just use
if (InMessage.message.startsWith("`highlight")) {
// whatever
}

What is the meaning of the three dots in a method declaration? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java, 3 dots in parameters
public static void getImages(String... folders) throws IOException{
}
In the above getImages() method, why there is three dots. What is it mean? I searched google but couldn't find anything.
Yeah, punctuation is hard to search for if you don't know the technical term. In this case, it is varargs. Here is a nice link to explain it. Basically, the caller can add as many arguments as desired, and the method sees them arriving as an array of that length.

Categories

Resources