Randomly Assign Array Elements?

Put your problem here if it does not fit any of the other categories.

Randomly Assign Array Elements?

Postby ramgraph1 » Sun Feb 03, 2008 11:49 pm

Is there an easy way to randomly assign array elements to objects so that each object in a series has a different element? So instead of the code below, object1 would randomly be assigned one of the elements, object2 a different element and so on until each of the five objects and five array elements are all randomly paired?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. object1 = (objectcount[0]);
  2.  
  3. object2 = (objectcount[1]);
  4.  
  5. object3 = (objectcount[2]);
  6.  
  7. object4 = (objectcount[3]);
  8.  
  9. object5 = (objectcount[4]);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Top

Postby plusminus » Mon Feb 04, 2008 1:31 am

Hello ramgraph,

Looks like you have an Array of Objects before :?: so you could shuffle it before assigning its elements to the objects. Done like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 String[] objArr = {"A","B","C","D"};
  2.                 ArrayList<String> objList = Lists.newArrayList(objArr);
  3.                 Collections.shuffle(objList );
  4.                
  5.                 objList.toArray(objArr);
  6.                 // objArr is now shuffled
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Last edited by plusminus on Mon Feb 04, 2008 9:56 am, edited 2 times in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ramgraph1 » Mon Feb 04, 2008 9:22 am

Wow. That's perfect! So simple and yet so beyond what I knew...
Thanks once more!
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Postby plusminus » Mon Feb 04, 2008 10:00 am

Hello ramgraph,

the java.util.Collections-Classcontaisn some useful static methods for List/Collections-Stuff.
Like: shuffle, reverse, swap, min, max, frequency and of course sort...

I love that Android function: "Lists.newArrayList(anyArray);" :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ramgraph1 » Mon Feb 04, 2008 10:41 pm

Shuffle is working great. But there is something that I was hoping to do with it that didn't work the way I thought it might. With one of the arrays that I am shuffling, I wanted to assign the array's elements to some objects' images. I defined a string array:

Code: Select all
String [] objects = {"red","blue","green","yellow","purple"};


and I tried this:
Code: Select all
door1.setImageResource(R.drawable.(objects[0]));


I have pngs with the names of the elements of the string array and this code works fine:
Code: Select all
door1.setImageResource(R.drawable.red);

Is there a way to get these array elements into the drawable reference so that I can have them randomly assigned?
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Postby plusminus » Mon Feb 04, 2008 11:21 pm

Hello ramgraph,

wohoo this is really weird if you think about it for one moment :!:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. door1.setImageResource(R.drawable.(objects[0]));
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

If we had a very very very very very very... intelligent programming language it would probably understand what you want to do (your thought is basically right, but 100% impossible the way you tried :!:).

There is an Android built in function, called getIdentifier, which transforms the name of a resource to the ID it got assigned.
:idea: Sample call:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int resID = getResources().getIdentifier("myImage", "drawable", "org.anddev.android.testproject");
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

For you it would be something like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. door1.setImageResource(getResources().getIdentifier(objects[0], "drawable", "your.package.structure"));
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

Let us know if it helped you. :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby ramgraph1 » Tue Feb 05, 2008 12:22 am

That worked perfectly and helps out a lot. Thanks.
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests