Affichage des articles dont le libellé est java. Afficher tous les articles
Affichage des articles dont le libellé est java. Afficher tous les articles

mardi 22 mars 2016

Java, First Item of ArrayList bugs when using String equals method

I have following classes (unnecessary details omitted):

class Person {
    public String name;
    public int id;

    public Person(String name, int id){
        this.name = name;
        this.id = id;
    }

    public void whoAmI(){
        System.out.println("I'm a person!");
    }
}

class Student extends Person {

    public Student(String name, int id){
        super(name, id);
    }

    public void whoAmI(){
        System.out.println("I'm a student!");
    }
}

class Teacher extends Person {

    public Student(String name, int id){
        super(name, id);
    }

    public void whoAmI(){
        System.out.println("I'm a teacher!");
    }

}

Now, I have an ArrayList, which holds all Teachers and Students. For example:

ArrayList<Person> people = new ArrayList<Person>();
Teacher t = new Teacher("John", 1);
people.add(t);
Student s = new Student("Dave", 2);
people.add(s);

At this point, I'm searching the arraylist and try to find out if a person with a specific name is a student or a teacher.

Person p = null;
for(int i=0; i<people.size(); i++){
    p = people.get(i);
    if(p.name.equals("John")){
        break;
    }
}

p.whoAmI();

As you can see, this should print

I'm a Teacher!

But it always prints

I'm a Student!

If I add the student first, or another item, it works correctly. But when the searched item was the first element of the arraylist, equals() method simply not working.

What's happening here?



Java, First Item of ArrayList bugs when using String equals method

jeudi 16 juillet 2015

Cannot query MongoDb on producer endpoint



Cannot query MongoDb on producer endpoint

SSRS 2010 - Report header missing on txt export



SSRS 2010 - Report header missing on txt export

Inserting javascript into div



Inserting javascript into div

.NET5 on OSX - C# errors in DNVM shell (so close..)



.NET5 on OSX - C# errors in DNVM shell (so close..)

Doxygen not parsing out reference link in markdown



Doxygen not parsing out reference link in markdown

Getting output from a PHP shuffle and INSERT into new database table



Getting output from a PHP shuffle and INSERT into new database table

how to parse nested json array in java



how to parse nested json array in java

Aggregate function repeats values



Aggregate function repeats values

Setup e2e tests with jenkins protractor and grunt



Setup e2e tests with jenkins protractor and grunt

creating a website for machine design calculations



creating a website for machine design calculations

Phonegap Pinlock, Html5 layer/Java layer makes a difference?



Phonegap Pinlock, Html5 layer/Java layer makes a difference?

confusion of char* str="ab", str and &str



confusion of char* str="ab", str and &str

ExtJS--submit date as timestamp



ExtJS--submit date as timestamp

Block until API result cached, then re-instantiate API class



Block until API result cached, then re-instantiate API class

getting values outof a loop (Clojure)



getting values outof a loop (Clojure)

Can you perform dialog navigation on a child window?



Can you perform dialog navigation on a child window?

GET working with Postman, but not with Ajax?



GET working with Postman, but not with Ajax?

Counting Frequencies Using (logical?) Expressions



Counting Frequencies Using (logical?) Expressions

How to execute two procedures or commit inside one to execute a INSERT in a table in part 1 of the script and use the table in part 2



How to execute two procedures or commit inside one to execute a INSERT in a table in part 1 of the script and use the table in part 2