solid.py
solid.py

Reputation: 2812

Unexpected Redefinition of variable in iterator to read all list element

As part of an exercise I made 3 objects from a pre-given class called MarriedPerson

I made an array instance for these 3 objects, then defined them manually due to their different properties. The purpose of the exercise was to add the entire array in a List, then read the list using Iterator object,
but I am getting an unexpected error in this line
MarriedPerson mp = (MarriedPerson)iter.next();

Error Code:Variable mp is already defined in method main(java.lang.String[])
(sorry for this kind of question but I am a beginner in Java)

import java.util.*;
abstract class MyTester {
  public static void main(String[] args){
   MarriedPerson[] mp = new MarriedPerson[3];
   mp[0] = new MarriedPerson("Tront", "Betty", 31, 980.5f, Person.FEMALE, 3); 
   mp[1] = new MarriedPerson("Tront", "Kirk", 31, 2080f, Person.MALE, 2); 
   mp[3] = new MarriedPerson("Tront", "Sonia", 31, 600f, Person.FEMALE, 0); 
   List<MarriedPerson> mpList = new ArrayList<MarriedPerson>(3);
   for( int i=0; i<3; i++ )
        mpList.add(mp[i]);

   Iterator iter = mpList.iterator();
   while(iter.hasNext()){
        MarriedPerson mp = (MarriedPerson)iter.next();
        mp.printInfo();
   }

   for( int i=0; i<3; i++ )
        mp[i].printInfo();
   mp[0].setSalary(mp[1]); 
   mp[0].printInfo(); 
   mp[0].setSalary(mp[2]); 
   mp[0].printInfo(); 
   mp[0].setSalary(mp[1].getSalary()); 
   mp[0].printInfo();
  }
}

Upvotes: 0

Views: 294

Answers (3)

Alexis C.
Alexis C.

Reputation: 93872

but I am getting an unexpected error in this line MarriedPerson mp = (MarriedPerson)iter.next();

Error Code:Variable mp is already defined in method main(java.lang.String[])

Because you already have an array named mp, which has a larger scope than the temporary variable mp you defined in your while loop.

As the JLS states:

It is a compile-time error if the name of a local variable v is redeclared as a local variable of the directly enclosing method, constructor, or initializer block within the scope of v;

Like this is not valid:

public static void main(String[] args) {
    int i;
    for (int i = 0; i < 10; i++)
        System.out.println(i);
}

This is not too:

 MarriedPerson[] mp = new MarriedPerson[3];

 /.../
 Iterator iter = mpList.iterator();
 while(iter.hasNext()){
     MarriedPerson mp = (MarriedPerson)iter.next(); //hey already an array named mp
     mp.printInfo();
 }

To get rid of this, you could give another name to the married person you get with your iterator:

while(iter.hasNext()){
    MarriedPerson marriedPerson = (MarriedPerson)iter.next();
    marriedPerson.printInfo();
}

Also don't use a raw Iterator.

Iterator<MarriedPerson> iter = mpList.iterator();

Upvotes: 1

CodeMonkey
CodeMonkey

Reputation: 23748

Local variable "mp" is already defined in main method:

 MarriedPerson[] mp = new MarriedPerson[3]

So you cannot declare a local variable with the same name:

 Iterator iter = mpList.iterator();
 while(iter.hasNext()){
        MarriedPerson mp = (MarriedPerson)iter.next(); // not allowed
        mp.printInfo();
   }

Change the variable name inside the loop and make it unique:

while(iter.hasNext()){
        MarriedPerson person = (MarriedPerson)iter.next();
        person.printInfo();
}

Or use Java's "for-each" loop construct which uses the underlying iterator mechanism:

for (MarriedPerson person : mpList) {
        person.printInfo();
}

Upvotes: 1

GoldenJam
GoldenJam

Reputation: 1510

You have defined the variable mp twice.

Once as:

MarriedPerson mp = (MarriedPerson)iter.next();

Then as:

MarriedPerson[] mp = new MarriedPerson[3];

Upvotes: 0

Related Questions