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 › create  array of strings which do not duplica
Page Index Toggle Pages: 1
create  array of strings which do not duplica (Read 379 times)
create  array of strings which do not duplica
Sep 13th, 2008, 12:18am
 
hi
just trying to figure out a way to run a for loop of strings adding new ones that are encountered to a new array and skipping those that have already been added. How would you do this? How do you check for something already inside an array?
Re: create  array of strings which do not duplica
Reply #1 - Sep 13th, 2008, 1:11am
 
You might want to use a Set of some kind - this, by definition, only stores unique values and will not add repeats to the collection. For example,

Quote:
void setup()
{
  // Create a set and add some items.
  HashSet mySet = new HashSet();
  mySet.add("Cat");
  mySet.add("Dog");
  mySet.add("Cat");  // This will not get added.
  
  // Display all the items stored in the set.
  Iterator i = mySet.iterator();
  while (i.hasNext())
  {
    println(i.next());
  }
}

Page Index Toggle Pages: 1