• Welcome to Smashboards, the world's largest Super Smash Brothers community! Over 250,000 Smash Bros. fans from around the world have come to discuss these great games in over 19 million posts!

    You are currently viewing our boards as a visitor. Click here to sign up right now and start on your path in the Smash community!

Java Array Problem

NintendoMan07

Smash Journeyman
Joined
Jul 23, 2008
Messages
251
Location
Dallas: The Land that Killed Me
I'm urgently needing to turn in a project for a class that's already late as is, but I'm stuck.

// Clear the tree. Create an array P of 500 integers with values 1, 2, 3, ..., 500
// Shuffle the array randomly. Then,
// for(int i=0;i<5000; i++) {
// Pick a randomly positioned integer from the array P
// and insert that value into the tree.
// Pick a randomly positioned integer from the array P
// and delete that value from the tree, if it is present.
// Don't print anything during the operation.
// }

How would I write the code for the stuff in bold?

If I can figure out how to do the random shuffling and picking at random, the rest of it I have some idea how to do.

As for the full context of the project, this part utilizes a binary search tree (not from the Java Collections Framework), and there's different commands involved. This is one of them.

And yeah, I've been in college learning Java for two years, so weep for the future of humanity if this ends up being a really stupid thing to be stuck on.
 

AltF4

BRoomer
BRoomer
Joined
Dec 13, 2005
Messages
5,042
Location
2.412 – 2.462 GHz
Use the Java Random class.

//Import the class
import java.util.Random;

//Create an object of the Random type
Random RandomCreator = new Random();

//create a random integer in the range 0-4999
int randomNumber = RandomCreator.nextInt( 5000 );

//make as many as necessary.


To shuffle an array randomly, you might think of iterating through the entire array, and swap the current place with a random place. Like this...
int randomNumber = RandomCreator.nextInt( 5000 );
int tempSwap;
for(int i = 0; i < 5000; i++)
{
tempSwap = arrayP[ randomNumber];
arrayP[ randomNumber ] = arrayP[ i ];
arrayP[ i ] = tempSwap;

randomNumber = RandomCreator.nextInt( 5000 );

}

Does that help?
 

NintendoMan07

Smash Journeyman
Joined
Jul 23, 2008
Messages
251
Location
Dallas: The Land that Killed Me
Wow, thanks!

I'll be honest, I should've actually thought of that, but I guess despairing over late work doesn't help me think at all.

Anyway, yeah, that works somewhat. I'm still having to debug my code, because my prof gave me some test files and my files are coming out different from his. It's irritating.
 
Top Bottom