-1

I have the following code

 int Number_Decision_Variables=State_Vector.nextInt();
        int max=0;
        int[] Num_Alt_Decision_variable=new int[Number_Decision_Variables];
        for(int Num=0;Num<Number_Decision_Variables;Num++){
            System.out.println("Enter the number of Alternatives for Decision Variable"+Num+1);
            Num_Alt_Decision_variable[Num]=State_Vector.nextInt();
            if(Num_Alt_Decision_variable[Num]>max)max=Num_Alt_Decision_variable[Num];
        }
        double[][] c=new double[Number_Decision_Variables][max];

I defined matrix c to have a number of columns equal to the maximum number in the "Num_Alt_Decision_variable" array. But I don't think this is a good practice because the cells in the "Num_Alt_Decision_variable" array ranges from 1 to 20 therefore I need to define a separate 1-d array for each "Number_Decision_Variables" with size equal to the value of in each cell in the "Num_Alt_Decision_variable" for example if Num_Alt_Decision_variable[1]=1 then the first array should have size of one, also if Num_Alt_Decision_variable[2]=10 then the second array should have size of ten any suggestion?

  • 2
    Just a suggestion: Dont use underscores for normal variables. Use only for `static` `final` variables. This is not at all readable – Polynomial Proton Feb 02 '15 at 07:40
  • possible duplicate of [Declare array in Java?](http://stackoverflow.com/questions/1200621/declare-array-in-java) – Raedwald Feb 02 '15 at 07:46

1 Answers1

0

You could create the 2D array dynamically, each row having a different number of columns :

double[][] c=new double[Number_Decision_Variables][];
c[0] = new double[Num_Alt_Decision_variable[0]];
c[1] = new double[Num_Alt_Decision_variable[1]];
...

Or you can use a loop to do the same.

for (int i = 0; i < c.length; i++)
    c[i] = new double[Num_Alt_Decision_variable[i]];
Eran
  • 359,724
  • 45
  • 626
  • 694