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 › how to: delete items from array
Page Index Toggle Pages: 1
how to: delete items from array (Read 687 times)
how to: delete items from array
Apr 9th, 2008, 12:06am
 
I have an array of strings and I want to randomly choose each item which I have done below. However, what I'm having trouble with is once a random item has been selected, what are the ways I can delete the item once it has been selected?

For instance when I click on the app the letter "C" comes up and when I click again it randomly goes through each item. How do I once I click the second time get rid of item "C" so that it doesn't appear in my array after it has been viewed?

code below...

Code:

int lotteryLength = 37;

String[] lottery = {
 "A", "B", "C", "D", "E",
 "F", "G", "H", "I", "J",
 "J", "K", "L", "M", "N",
 "O", "P", "Q", "R", "S",
 "T", "U", "V", "W", "X",
 "Y", "Z",
 "1", "2", "3", "4", "5",
 "6", "7", "8", "9", "10",
};

PFont gotham;
int pointer = 0;
int rand;
boolean switcher = false;

void setup() {
 size(200,200);
 smooth();
 gotham = loadFont("gotham.vlw");
 textFont(gotham, 100);
}

void draw() {
 background(103);
 noStroke();
 fill(0);
 textAlign(CENTER);

 if(switcher) {
   pointer = rand;
   text(lottery[pointer], width/2,height/2 + 36);
 }
 else {
   text(lottery[pointer], width/2, height/2 + 36);
   pointer = int(random(0, lotteryLength));
 }
}

void mousePressed() {
 rand = int(random(0, lotteryLength));

 if(switcher) {
   switcher = false;
   
   //DELETE THIS TO NOT MAKE
   //ITEMS IN ARRAY LOST
   
   if(lotteryLength >= 0) {
     lotteryLength--;
   }
   
   //END COMMENTARY
   
   
 }
 else {
   switcher = true;
 }
}

Re: how to: delete items from array
Reply #1 - Apr 9th, 2008, 7:16pm
 
dont know if this can help you

Quote:


String[] array = { "a","b","c","d","e","f","g","h","i" };

int del = int(random(array.length));
println(del);

for(int i=del ; i<array.length-1 ; i++){
 array[i] = array[i+1];
}
array = shorten(array);

println(array);
 

Re: how to: delete items from array
Reply #2 - Apr 13th, 2008, 1:24am
 
thanks, I sort of got it working with that, but found that this was much more succinct.

Code:
import processing.opengl.*;

int lotteryLength = 37;

String[] lottery = {
"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S",
"T", "U", "V", "W", "X",
"Y", "Z",
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
};

PFont gotham;
int pointer = 0;
int rand;
boolean switcher = false;

void setup() {
size(200,200, OPENGL);
smooth();
gotham = loadFont("gotham.vlw");
textFont(gotham, 100);
}

void draw() {
//background(103);
noStroke();

textAlign(CENTER);
}

void mouseReleased() {
do {
rand = int(random(0, lotteryLength));
}
while(lottery[rand] == "");
println(lottery[rand]);
fill(103);
rect(0,0,width,height);
fill(0);
text(lottery[rand], width/2,height/2 + 36);
lottery[rand] = "";
}
Re: how to: delete items from array
Reply #3 - Apr 14th, 2008, 10:25pm
 
to use that method, set the array element to null instead of "". using == with strings only compares the object:
http://processing.org/reference/troubleshooting/index.html#strings

your code works as it is because the compiler is making "" a single String object that gets used multiple places in your code. with another compiler it may not even work.
Re: how to: delete items from array
Reply #4 - Apr 18th, 2008, 10:27pm
 
wow, that is wild. That makes a lot of sense, however, I'm still having some issues with null, because I get a null pointer exception when the array hits null I can never get through the entire array randomly.

I assume that I would have to reverse my logic and create a hierarchy that would be something along the lines of...?

Code:


if(lottery[rand].equals("any_real_string")) {
//do nothing
}
else {
//randomize_the_leftover_items_in_the_array
}

Re: how to: delete items from array
Reply #5 - Apr 21st, 2008, 8:07pm
 
Hello!
Is there a way to using vector container from java, in processing?
thanks for the reactions.
Re: how to: delete items from array
Reply #6 - Apr 21st, 2008, 8:10pm
 
Yes, you can just use them. (You can use any Java 1.4 classes in Processing)
Page Index Toggle Pages: 1