We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › When filled with loop, ArrayList doesn't match
Page Index Toggle Pages: 1
When filled with loop, ArrayList doesn't match (Read 315 times)
When filled with loop, ArrayList doesn't match
Jul 13th, 2008, 10:02am
 
This code gets data from a ArrayList and compares it with the 'original' data, also in an ArrayList (happends every 20 seconds). If I fill the ArrayList manually, the match works perfectly. If I fill the ArrayList with a for loop, the two ArrayList are identical before they go in the 'match-section' of the code, but somehow they just don't match. The first time, the orginal ArrayList is stil empty, the second loop it has values (That is correct). drawData collects the info, compareList makes the comparison, they are both called by a thread which is called in the setup. Here is the code:

import processing.xml.*;
import java.util.ArrayList;

XMLElement xml;
ArrayList participants;
ArrayList list1;
ArrayList list2;


void setup() {
 size(900, 600); //needs to be First
 
 //setting up the font
 PFont font = loadFont("Avenir-Book-25.vlw");
 textFont(font);

 list1 = new ArrayList();

 //****** Threads
 new Slurper();

}

void draw() {
 background(#000000);
 noStroke();
 smooth();
}


void getData() {
 list2 = new ArrayList();


 xml = new XMLElement(this, "test.xml");
 XMLElement[] name = xml.getChildren("person");
 for (int z = 0; z < name.length; z++) {
   String participantName = name[z].getContent();
   list2.add(participantName);
 
  }

}

void compareLists(){
 println("list1 size: "+list1.size());
 println("list1 participant: "+ list1);
 println("list2 participant: "+ list2);
 
 // Loop list 1 to Remove old Users (not in list2)
 for (int i=0; i < list1.size(); i++){
   String list1User = (String)list1.get(i);
   boolean userStillExsist = false;

 println("list1 size: "+list1.size());
 println("list2 size: "+list2.size());

   for (int j=0; j < list2.size(); j++){
     String list2User = (String)list2.get(j);
      println("list1 participant: "+ list1User);
      println("list2 participant: "+ list2User);
 
     if(list1User == list2User){
       println("These users matched " + list1User +" == "+ list2User);
       userStillExsist = true;
     }

    }
     if(userStillExsist == false){
       list1.remove(i);
       println("user "+i+" removed name= "+list1User);  
     }
     
   }  

 // Loop list 2 to Add new Users (not in list1)
 for (int i=0; i < list2.size(); i++){
   String list2User = (String)list2.get(i);
   boolean userAlreadyInList = false;

   for (int j=0; j < list1.size(); j++){
     String list1User = (String)list1.get(j);
     
     if(list2User == list1User){
       println("userAlreayInList " + list2User);
       userAlreadyInList = true;
     }
    }
     if(userAlreadyInList == false){
       list1.add(list2User);
       println("user "+list2User+" added");  
     }
     
   }  

for (int i=0; i < list1.size(); i++){
   String list1User = (String)list1.get(i);
   println("tobeShure "+list1User);
 }
 
}  

//__________________________________________Thread

class Slurper implements Runnable{
   
 Slurper(){
   Thread thread = new Thread(this);
   thread.start();
 }
 
 public void run () {
   try{
     int timeOut = 20; //TimeOut in sec
    while(true){
      getData();
      compareLists();
      Thread.sleep(timeOut * 1000);
    }
   }
   
   catch (InterruptedException e) {
     e.printStackTrace();
   }
   
 }
}
Re: When filled with loop, ArrayList doesn't match
Reply #1 - Jul 13th, 2008, 10:03am
 
Now here is the console feedback when the ArrayList is filled manually and it works fine:

list1 size: 0
list1 participant: []
list2 participant: [test1, test2, test3, test4, test5]
user test1 added
user test2 added
user test3 added
user test4 added
user test5 added
tobeShure test1
tobeShure test2
tobeShure test3
tobeShure test4
tobeShure test5
---------------------------Enters the second loop here
list1 size: 5
list1 participant: [test1, test2, test3, test4, test5]
list2 participant: [test1, test2, test3, test4, test5]
list1 size: 5
list2 size: 5
list1 participant: test1
list2 participant: test1
These users matched test1 == test1
list1 participant: test1
list2 participant: test2
list1 participant: test1
list2 participant: test3
list1 participant: test1
list2 participant: test4
list1 participant: test1
list2 participant: test5
list1 size: 5
list2 size: 5
list1 participant: test2
list2 participant: test1
list1 participant: test2
list2 participant: test2
These users matched test2 == test2
list1 participant: test2
list2 participant: test3
list1 participant: test2
list2 participant: test4
list1 participant: test2
list2 participant: test5
list1 size: 5
list2 size: 5
list1 participant: test3
list2 participant: test1
list1 participant: test3
list2 participant: test2
list1 participant: test3
list2 participant: test3
These users matched test3 == test3
list1 participant: test3
list2 participant: test4
list1 participant: test3
list2 participant: test5
list1 size: 5
list2 size: 5
list1 participant: test4
list2 participant: test1
list1 participant: test4
list2 participant: test2
list1 participant: test4
list2 participant: test3
list1 participant: test4
list2 participant: test4
These users matched test4 == test4
list1 participant: test4
list2 participant: test5
list1 size: 5
list2 size: 5
list1 participant: test5
list2 participant: test1
list1 participant: test5
list2 participant: test2
list1 participant: test5
list2 participant: test3
list1 participant: test5
list2 participant: test4
list1 participant: test5
list2 participant: test5
These users matched test5 == test5
userAlreayInList test1
userAlreayInList test2
userAlreayInList test3
userAlreayInList test4
userAlreayInList test5
tobeShure test1
tobeShure test2
tobeShure test3
tobeShure test4
tobeShure test5


--------------------------------Second feedback file

Here is the console feedback when the Arraylist is filled with the loop you currently see in the code. Also tried to fill it with a simple loop with intergers, and then it has the same problem.

list1 size: 0
list1 participant: []
list2 participant: [Barry Borsboom, Dennis Koks, Bob Karreman, Peter van Waard]
user Barry Borsboom added
user Dennis Koks added
user Bob Karreman added
user Peter van Waard added
tobeShure Barry Borsboom
tobeShure Dennis Koks
tobeShure Bob Karreman
tobeShure Peter van Waard
---------------------------Enters the second loop here
list1 size: 4
list1 participant: [Barry Borsboom, Dennis Koks, Bob Karreman, Peter van Waard]
list2 participant: [Barry Borsboom, Dennis Koks, Bob Karreman, Peter van Waard]
list1 size: 4
list2 size: 4
list1 participant: Barry Borsboom
list2 participant: Barry Borsboom
list1 participant: Barry Borsboom
list2 participant: Dennis Koks
list1 participant: Barry Borsboom
list2 participant: Bob Karreman
list1 participant: Barry Borsboom
list2 participant: Peter van Waard
user 0 removed name= Barry Borsboom
list1 size: 3
list2 size: 4
list1 participant: Bob Karreman
list2 participant: Barry Borsboom
list1 participant: Bob Karreman
list2 participant: Dennis Koks
list1 participant: Bob Karreman
list2 participant: Bob Karreman
list1 participant: Bob Karreman
list2 participant: Peter van Waard
user 1 removed name= Bob Karreman
user Barry Borsboom added
user Dennis Koks added
user Bob Karreman added
user Peter van Waard added
tobeShure Dennis Koks
tobeShure Peter van Waard
tobeShure Barry Borsboom
tobeShure Dennis Koks
tobeShure Bob Karreman
tobeShure Peter van Waard
Re: When filled with loop, ArrayList doesn't match
Reply #2 - Jul 13th, 2008, 12:55pm
 
When comparing strings, you must use stringA.equals(stringB) NOT stringA == stringB

The reason your manually filled one probably works is that you're making list1 and list2 entries from the same String object, and so the comparison will work, but only because it compares object references, and not the contents of the Strings themselves.
Re: When filled with loop, ArrayList doesn't match
Reply #3 - Jul 13th, 2008, 1:40pm
 
Thx! That does make the match, however... After the match is made, the variable is still removed from the ArrayList. Also later on when adding new content, the match is made and than it will still add the same variable despite having found a match. Know why? Here is some test code:

Quote:


import processing.xml.*;
import java.util.ArrayList;

ArrayList list1;
ArrayList list2;

String list1User;
String list2User;


void setup() {
 size(100, 100); //needs to be First
 
 getData();
 compareLists();
}


void getData() {
 list2 = new ArrayList();
  list1 = new ArrayList();
 
 int count = 0;
 int correct = 1;
 while (count < 6) {
   list1.add("test " + count++);
   list2.add("test " + (count-correct));
 }

}

void compareLists(){
 println("list1 size: "+list1.size());
 println("list1 participant: "+ list1);
 println("list2 participant: "+ list2);
 
 // Loop list 1 to Remove old Users (not in list2)
 for (int i=0; i < list1.size(); i++){
   String list1User = (String)list1.get(i);


 println("list1 size: "+list1.size());
 println("list2 size: "+list2.size());

   for (int j=0; j < list2.size(); j++){
     String list2User = (String)list2.get(j);
      println("list1 participant: "+ list1User);
      println("list2 participant: "+ list2User);
 
     if(list1User.equals(list2User) == true){
       println("These users matched " + list1User +" == "+ list2User);
     }
   }
   
     if(list1User.equals(list2User) == false){
       list1.remove(i);
       println("user "+i+" removed name= "+list1User);  
     }

   }  

 // Loop list 2 to Add new Users (not in list1)
 for (int i=0; i < list2.size(); i++){
   String list2User = (String)list2.get(i);

   for (int j=0; j < list1.size(); j++){
     String list1User = (String)list1.get(j);

      if(list2User.equals(list1User) == true){
       println("userAlreayInList " + list2User);
     }
    }
     if(list2User.equals(list1User) == false){
       list1.add(list2User);
       println("user "+list2User+" added");  
     }
     
   }  

for (int i=0; i < list1.size(); i++){
   String list1User = (String)list1.get(i);
   println("tobeShure "+list1User);
 }
 
}  
   


Page Index Toggle Pages: 1