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 › ArrayList of objects and splice
Page Index Toggle Pages: 1
ArrayList of objects and splice (Read 843 times)
ArrayList of objects and splice
Dec 13th, 2009, 8:51am
 
I cannot seem to get an ArrayList of objects to splice in new objects. I think it's just a syntax thing. I am trying to maintain a list of active players and a wait list. I want to be able to pull any player out of playerList by offset and append to the waitList. Order matters in playerList but It doesn't matter on waitList so I can do a simple append. I've tried splice as Player object and I also tried converting to an ArrayList as seen below. I can't seem to get either to work.

Can anyone shed light?

Object code:

Quote:
class Player
{
  String name;
  String img;
  int status;
  
  Player(String s, String t, int b)
  {
    name = s;
    img = t;
    status = b;
  }
}



Code:

Quote:
ArrayList playerList;
ArrayList waitList;
ArrayList tmpList;
 
void setup()
{
 size(400, 200);
 frameRate(30);
 playerList = new ArrayList();
 waitList = new ArrayList();
 tmpList = new ArrayList();

 playerList.add(new Player("Kahlan", "Kahlan.png", 0));
 playerList.add(new Player("Thunderstroke", "Thunderstroke.png", 0));
 playerList.add(new Player("Bellis", "Bellis.png", 0));
 Player tmp = (Player) playerList.get(1);
 playerList.remove(1);
 waitList.add(tmp);
 
 Player tmp2 = (Player) waitList.get(0);
 waitList.remove(0);
 tmpList.add(tmp2);

/* This is the non-working bit
 playerList = (ArrayList[]) splice(playerList, tmpList, 0);
 */
}

void draw()
{

 fill(#ff0000);
 
 for (int i=0; i < playerList.size(); i++) {
   Player p = (Player) playerList.get(i);
   println(p.name);
 }
}

Re: ArrayList of objects and splice
Reply #1 - Dec 13th, 2009, 11:28am
 
The splice() command works with arrays and you are using ArrayList.

ArrayList has a modified version  of the add

Code:
myarraylist.add(pos, myobject); 



Where pos is a valid position in myarraylist and myobject is the thing to insert.

Page Index Toggle Pages: 1