107

I want to have two loop variables of different types. Is there any way to make this work?

@Override
public T get(int index) throws IndexOutOfBoundsException {
    // syntax error on first 'int'
    for (Node<T> current = first, int currentIndex; current != null; 
            current = current.next, currentIndex++) {
        if (currentIndex == index) {
            return current.datum;
        }
    }
    throw new IndexOutOfBoundsException();
}
4
  • What is first? It isn't declared anywhere. Is it a class member?
    – extraneon
    Commented Aug 22, 2010 at 19:37
  • 8
    You should accept one answer below Commented Sep 8, 2013 at 17:50
  • And unlike C, Java does not have the comma operator: stackoverflow.com/questions/12601596/… , which would allow to initialize (but not declare) two variables of different types. Commented Feb 26, 2015 at 21:56
  • @Nick Heiner Could you please mark one of the below answers as accepted? Commented Jun 26, 2018 at 13:43

4 Answers 4

121

The initialization of a for statement follows the rules for local variable declarations.

This would be legal (if silly):

for (int a = 0, b[] = { 1 }, c[][] = { { 1 }, { 2 } }; a < 10; a++) {
  // something
}

But trying to declare the distinct Node and int types as you want is not legal for local variable declarations.

You can limit the scope of additional variables within methods by using a block like this:

{
  int n = 0;
  for (Object o = new Object();/* expr */;/* expr */) {
    // do something
  }
}

This ensures that you don't accidentally reuse the variable elsewhere in the method.

3
  • 14
    Anyone have any idea why the language designers implemented this seemingly unnecessary constraint? Commented May 5, 2011 at 15:44
  • @glenviewjeff - that would be best asked as a separate question.
    – McDowell
    Commented May 5, 2011 at 21:17
  • 4
    @JeffAxelrod, maybe for historical reasons because Java was modeled after C++... see this post: stackoverflow.com/questions/2687392/… Commented Jul 3, 2013 at 9:58
23

You can't like this. Either you use multiple variables of the same type for(Object var1 = null, var2 = null; ...) or you extract the other variable and declare it before the for loop.

12

Just move variable declarations (Node<T> current, int currentIndex) outside the loop and it should work. Something like this

int currentIndex;
Node<T> current;
for (current = first; current != null; current = current.next, currentIndex++) {

or maybe even

int currentIndex;
for (Node<T> current = first; current != null; current = current.next, currentIndex++) {
4
  • 2
    Neither will compile: you have to initialize variables before use.
    – unbeli
    Commented Aug 22, 2010 at 19:06
  • @unbeli well, I wasn't exercising in manual code compilation :) I just wanted to give the idea. Commented Aug 22, 2010 at 19:30
  • 5
    @unbeli: Just to clarify: currentIndex needs to be initialized. The first thing Nikita does to it is "currentIndex++", which naturally brings up the question, increment what? current is fine because the first use is to set it to first.
    – Jay
    Commented Aug 27, 2010 at 17:22
  • Usually to better a write an incrementation in a for loop, one should use ++var as the notation var++ required from the compiler to duplicate the var's content before incrementing it to return it as the result of the expression although nobody ever wants that. Of course, compiler will optimize that out but it is like throwing trash on the road waiting for others to clean-up.
    – Chucky
    Commented Sep 6, 2013 at 17:13
6

Variables declared in the initialization block must be of same type

we can't initialize the different data types in the for loop as per their design. I'm just putting a small example.

for(int i=0, b=0, c=0, d=0....;/*condition to be applied */;/*increment or other logic*/){
      //Your Code goes here
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.