Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: How to delete random item from multi dimensional array
|
Posted: Nov 11, 2002 2:45 PM
|
|
class Randy
{
public static void deleteRandomCellInARow( Object [][] objects, int row )
{
objects[row][new java.util.Random().nextInt(objects[row].length)] = null;
}
public static void main( String [] args )
{
// Create a silly 2-d array:
String stuff [][] = {
{ "January", "February", "March" },
{ "April", "May", "June" },
{ "July", "August", "September" },
{ "October", "November", "December" }
};
// Use the incredible "deleting" technology:
deleteRandomCellInARow( stuff, 1 );
// Show the result:
for( int row = 0; row < stuff.length; row++ )
for( int col = 0; col < stuff[0].length; col++ )
System.out.println( stuff[row][col] );
}
}
|
|