Asked By
arturo owen
10 points
N/A
Posted on - 04/11/2012
How can I use multidimensional array for storing and retrieving data? I only need to store and retrieve 5 data temporarily so using SQL would be a waste of time, moreover, this is just for simulation. Can you give some examples?
Use Multidimensional Array for Storing Data
Hi Arthuro,
Â
Take a look at this code sample:
Â
Â
/**
 * Write a description of class d2_array here.
 *Â
 * @author (your name)Â
 * @version (a version number or a date)
 */
public class d2_array
{
  // instance variables – replace the example below with your own
  private int x;
Â
  /**
   * Constructor for objects of class d2_array
   */
  public d2_array()
  {
   String[][] Data;
Â
   Data = new String[5][5]; // 5 rows and 5 columns
Â
   //Assign the values, do it either dynamically or statically
Â
   //For first flow
Â
  // add random values to the 2d array
   Data[0][1] = "1";
   Data[0][2] = "5";Â
   Data[1][0] = "3";
   Data[1][1] = "7";
   Data[1][2] = "6";
Â
   //printing
Â
   for(int i=0;i<2;i++)
Â
   {
Â
     for(int j=0;j<3;j++)
Â
     {
 Â
       System.out.print(Data[i][j]+"n");
 Â
     }
Â
     //System.out.print("n");
    }
}
Â
I hope it helps.
Â
Â