The Artima Developer Community
Sponsored Link

Java Answers Forum
How to delete random item from multi dimensional array

1 reply on 1 page. Most recent reply: Nov 11, 2002 2:45 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
VP Rajesh

Posts: 1
Nickname: rvp
Registered: Nov, 2002

How to delete random item from multi dimensional array Posted: Nov 11, 2002 12:19 PM
Reply to this message Reply
Advertisement
Could you please tell me me how to delete an item from two dimesional array given the row index? Appreciate your time and effort.

-Raj


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
Reply to this message Reply
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] );
   }
}

Flat View: This topic has 1 reply on 1 page
Topic: Total Rows in ResultSet Previous Topic   Next Topic Topic: testing a stringtokenizer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use