0

I have the following two dimensional array in my java program.

Integer[][] arrayOfSets = {
        {1,2,5,6,9,10},
        {9,10,11,12},
        {1,2,3,4},
        {3,5,6,7,8},
        {9,10,11,12},
        {4,8},
};

Can someone show me the code on how to make the program dynamic, where the program asks the user to enter the row number and column number, i.e. how many arrays the two dimensional array should hold and then asks the user for how big each array and what are the numbers that each array should hold? Thanks so much in advance.

This is what I've tried so far:

 System.out.println("HOw many sets arrays would you like");
    int numArrays=sc.nextInt();
    Integer [][] arrayName = new Integer[numArrays][];
    for(int i=0;i<arrayName.length;i++){
        System.out.println("enter the size of the array number"+i);
        int sizeArray=sc.nextInt();
        for(int k=0;k<sizeArray;k++){
            System.out.println("enter element");
            int e=sc.nextInt();
            arrayName[i][k]=e;
        }

    }

Does this look right? Thanks for all the help in advance.

Ok I modified my cod again. Following is what I have now:

System.out.print("How many arrays would you like?"); int numSets=sc.nextInt(); Integer[][] result = new Integer[numSets][];

for (int k=0; k< numSets; k++){
  System.out.println("Enter the size of the array");
  int setSize  = sc.nextInt();

  Integer[] row = new Integer[setSize];
  for (int m=0; m< setSize; m++){
      System.out.println("enter element: ");
      row[m] = sc.nextInt();
  }
  result[k] = row;
}
Antal Spector-Zabusky
  • 36,191
  • 7
  • 77
  • 140
sap
  • 1,141
  • 6
  • 41
  • 62
  • Please see my code and let me know if that's correct. Thanks. – sap Nov 27 '10 at 00:03
  • An observation: Java does not support true multi-dimensional arrays; rather, it supports 1-dimensional arrays, where each element can itself be another 1-dimensional array. The difference is the way memory is allocated, where `a[i][j]` differs from `a[i,j]`. – David R Tribble Nov 27 '10 at 00:03
  • `Does this look right?` I don't know, what errors are you getting? – Falmarri Nov 27 '10 at 00:28
  • Already posted on SO.http://stackoverflow.com/questions/2707357/how-to-create-dynamic-two-dimensional-array-in-java – JDGuide May 15 '13 at 06:53

5 Answers5

4

What you have is almost correct. This:

Integer [][] arrayName = new Integer[numArrays][];

creates the first dimension, but you also need to create the individual second dimensions. After you read in the size of the each array:

int sizeArray=sc.nextInt();

you need to create the corresponding array:

arrayName[i] = new Integer[sizeArray];
casablanca
  • 69,683
  • 7
  • 133
  • 150
1

Here's a sample function you could adapt:

 Integer[][] createSampleArray()
 {
      final int N1 = 5;
      Random r = new Random();
      Integer[][] result = new Integer[N1][];
      for (int i = 0; i < N1; ++i)
      {
          final int N2 = r.nextInt();
          Integer[] row = new Integer[N2];
          for (int j = 0; j < N2; ++j)
          {
              row[j] = r.nextInt();
          }
          result[i] = row;
      }
      return result;
 }

Aha, your problem is you're not creating any 1-D arrays to put the elements in:

Integer [][] arrayName = new Integer[numArrays][];
    for(int i=0;i<arrayName.length;i++){
        System.out.println("enter the size of the array number"+i);
        int sizeArray=sc.nextInt();
        for(int k=0;k<sizeArray;k++){
            System.out.println("enter element");
            int e=sc.nextInt();
            arrayName[i][k]=e;
        }

    }

You can't assign arrayName[i][k] until you assign arrayName[i] first for each row:

arrayName[i] = new Integer[sizeArray];

Jason S
  • 184,598
  • 164
  • 608
  • 970
  • @user491753: the customary response when you like an answer is to upvote it and/or accept.... – Jason S Nov 27 '10 at 13:54
  • I have no idea how to do that. I keep on asking people to tell me how to do that but no one answers.... – sap Nov 27 '10 at 14:57
  • they're on the upper left corner of any of the answers: there's a vote count with an up arrow above it, and a down arrow below it, and you should also see an outline of a checkbox. click on the appropriate one to activate. – Jason S Nov 27 '10 at 15:01
1

You just prompt the user for the initial array sizes then initialize the array.

import java.util.Scanner;
public class blah {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("How Many Rows?");
        int rows = s.nextInt();
        System.out.println("How Many Colums?");
        int cols = s.nextInt();
        int [][] arrayOfSets = new int [rows] [cols];
    }
}
austinbv
  • 9,297
  • 6
  • 50
  • 82
0

@casablanca answer is perfect. You need to create the object of 'Integer' class in the end

 for(int j=0; j < arrayname[i].length; j++)
  arrayname[i][j] = new Integer(int_value);
Raghav
  • 766
  • 16
  • 24
0

Take a look at this SOq:

Community
  • 1
  • 1
icyrock.com
  • 27,952
  • 4
  • 66
  • 85