Sorting a two-dimensional integer array

I have a 2-D array that looks simliar to this:
1 0 0 0
1 1 0 0
2 0 0 0
2 1 1 1
1 2 1 0
2 1 2 0
3 1 1 1
4 1 0 0
2 1 1 2
.I am trying to come up with an algorithm that will sort this array by the first column, then the second, then the third, then the fourth. But I'm stumped. The array above should look like this when sorted:
1 0 0 0
1 1 0 0
1 2 1 0
2 0 0 0
2 1 1 1
2 1 1 2
2 1 2 0
3 1 1 1
4 1 0 0I've been able to sort 2-D arrays with only two columns, but this has been a new challenge for me. I've been able to come up with a brute force approach but it doesn't seem to work, nor is it very efficient. Any suggestions on how to approach this problem would be a great help.

An 2-D array is simply an array of arrays, so you should be able to use a simple Comparator:
class ArrayIntComparator implements Comparator
    public int compare(Object  o1, Object o2)
         int[] arr1 = (int[]) o1;
         int[] arr2 = (int[]) o2;
         for (int i=0; i<=arr1.length; ++i)
            if (arr1[i]  != arr2)
return(arr2[i] - arr1[i]);
return 0;
int[][] arr2d = ...
Arrays.sort(arr2d, new ArrayIntComparator());
Didn't compile it, but it oughta give you a close start.

Similar Messages

  • Sorting a two dimensional array in descending order..

    Guy's I'm a beginning Java programmer. I've been struggling with finding documentation on sorting a two dimensional primitive array in descending order.. The following is my code which goes works, in ascending order. How do I modify it to make the list write out in descending order?
    class EmployeeTable
         public static void main(String[] args)
         int[][] hours = {
              {2, 4, 3, 4, 5, 8, 8},
              {7, 3, 4, 3, 3, 4, 4},
              {3, 3, 4, 3, 3, 2, 2},
              {9, 3, 4, 7, 3, 4, 1},
              {3, 5, 4, 3, 6, 3, 8},
              {3, 4, 4, 6, 3, 4, 4},
              {3, 7, 4, 8, 3, 8, 4},
              {6, 3, 5, 9, 2, 7, 9}};
         int [] total = new int [hours.length];
         for (int i = 0; i < hours.length ; i++){
              for (int j = 0; j < hours.length ; j++){
                   total[i] += hours[i][j];
         int[] index = new int[hours.length];
         sortIndex(total, index);
    for (int i = 0; i < hours.length; i++)
    System.out.println("Employee " + index[i] + " hours are: " +
    total[i]);
    static void sortIndex(int[] list, int[] indexList) {
    int currentMax;
    int currentMaxIndex;
    for (int i = 0; i < indexList.length; i++)
    indexList[i] = i;
    for (int i = list.length - 1; i >= 1; i--) {
    currentMax = list[i];
    currentMaxIndex = i;
    for (int j = i - 1; j >= 0; j--) {
    if (currentMax < list[j]) {
    currentMax = list[j];
    currentMaxIndex = j;
    // How do I make it go in descending order????
    if (currentMaxIndex != i) {
    list[currentMaxIndex] = list[i];
    list[i] = currentMax;
    int temp = indexList[i];
    indexList[i] = indexList[currentMaxIndex];
    indexList[currentMaxIndex] = temp;

    Bodie21 wrote:
    nclow all you wrote was
    "This looks to me like a homework assignment that you're asking us to solve for you. Your description of what it does isn't even correct. It doesn't sort a 2d array.
    You would probably elicit better help if you could demonstrate that you've done some work and have something specific that you don't understand."
    To me that sounds completely sarcastic. You didn't mention anything constructive besides that.. Hence I called you a male phalic organ, not a rear exit...Bodie21, OK, now you've got 90% of us who regularly contribute to this forum thinking your the pr&#105;ck. Is that what you wanted to do? If so, mission accomplished. Many will be looking for your name next time you ask for help. Rude enough for you?

  • Using two dimensional character array in a Jolt-Tuxedo call

    Hi,
    I have a Tuxedo service that returns a two dimensional character array as follows:
    char airports[234][3] ;
    There are 234 rows and each row is a character array of 3 characters.
    When I call this service using Jolt, how can I access these in the Java program
    Can the getStringDef or getStringItemDef be used by any chance ?
    Thanks
    Bala.

    Hello Bala,
    R u using FML Buffers or Views???
    regards
    MS

  • Searching two dimensional string arrays

    Hello, I'm very new to Java. I would like some advice on the code below. What I'm trying to do is: I want the user to enter a 9 digit number which is already stored in an two dimensional array. The array is to be searched and the 9 digit number and corresponding name is to be printed and stored for future reference. Something is wrong with my array checking. If I enter the nine digit number, the program errors and asks me again for the number. If I enter 0-4, I receive an output. I just don't know how to compare string array values. Could someone please help me with this?
    import java.io.*;
    import java.util.*;
    public class SocialSn2
         public static void main(String args[]) throws Exception
              boolean validSSAN = false;
              Ssn(validSSAN);
              if (validSSAN)
                   //Ssn(false);//write the code here to continue processing
         }//end main
         public static void Ssn(boolean Validated)
              String[][] employees =
                   {{"333333333", "Jeff"},
                   {"222222222", "Keith"},
                   {"444444444", "Sally"},
                   {"555555555", "Kaylen"},
                   {"111111111", "Sheriden"}     };
              boolean found = false;
              while (!found)
                   System.out.println("Enter the employee's Social Security number.");
                   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                   try
                        String line = in.readLine();
                        int input = Integer.parseInt(line); //added
                        found = false;
                        for(int j = 0; j < 5; j++)
                        if(input >= 0 && input <= employees.length)
                             //if(employees[j][0].equals(input))
                                  //System.out.println(employees[input][0] + employees[input][1]);
                                  found = true;
                             System.out.println(employees[input][0] + " " + employees[input][1]);
                   catch (Exception exc)
                        System.out.println("error");
                        found = false;
              Validated = found;
         }//end Ssn
    }//end class

    There seems to be some problem with your loop for checking those values
    if you had used System.err.println() in your catch block you would see that you were getting an ArrayIndexOutOfBoundsException
    The following works for me.
    import java.io.*;
    import java.util.*;
    public class SocialSn2
    public static void main(String args[]) throws Exception
    boolean validSSAN = false;
    Ssn(validSSAN);
    if (validSSAN)
    //Ssn(false);//write the code here to continue processing
    }//end main
    public static void Ssn(boolean Validated)
    String[][] employees =
    {{"333333333", "Jeff"},
    {"222222222", "Keith"},
    {"444444444", "Sally"},
    {"555555555", "Kaylen"},
    {"111111111", "Sheriden"} };
    boolean found = false;
    System.out.println(employees.length);
    System.out.println(employees[0].length);
    while (!found)
    System.out.println("Enter the employee's Social Security number.");
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    try
    String line = in.readLine();
    //int input = Integer.parseInt(line); //added
    found = false;
    for (int i=0; i< employees.length;i++)
         if(employees[0].equals(line))
         System.out.println(employees[i][0]+" "+employees[i][1]);
         found = true;
    catch (Exception exc)
    System.err.println(exc);
    found = false;
    Validated = found;
    }//end Ssn
    }//end class

  • How to extract one dimension out of a two dimensiona​l array

    Hello,
    May be this question is too naive and simple.I have a two dimensional array (two columns and 256 rows). All I want is to extract one of these columns as a separate one dimensional array. It seems like a very very basic task that any programming language should address. However, I could not find the right funciton in the array functions list. I am sure I am missing something very obvious. I tried index array, array subset, array to cluster and reshape array. None of them is the right function for this purpose. I have used LabVIEW for a quite a while now, but still I can not find a solution to this basic problem. Can someone help me out.
    Thanks
    Solved!
    Go to Solution.

    Hi -
    With the Index Array  function have you tried wiring a number to the input labeled index (col)?
    See the attached vi for an example.
    Attachments:
    array index1.vi ‏6 KB

  • Sorting a two-dimensional array to check min and max random results.

    I am working on a new project to learn more advanced uses of java, and have run into some very confusing output. the program should roll 2 6 sided dice 36000 times, storing the results (how many times each of the 21 possible combinations of rolls occurred) in a 2-d array, printing that array in tabular format, then copying the values to a 1d array to sort numericallly (ascending) then print which combination of rolls occured least and most, with how many occurences for each (least and most) here is the code I have, the error will be below it.
    package d12;
    //import classes to use
    import java.util.Random;
    import java.util.Arrays;
    public class randomDice2D {
         * @param args the command line arguments
        public static void main(String[] args) {
    //bring in new Random object
            Random r = new Random();
    //declare all variables and arrays
            int[][] dieResults; dieResults = new int[6][6];
            int[] sortingResults; sortingResults=new int[37];
            int one;  int two;  int three;  int four;  int five;  int six;
            int seven;  int eight;  int nine;  int ten;  int eleven;  int twelve;
            int thirteen;  int fourteen;  int fifteen;  int sixteen;  int seventeen;
            int eighteen;  int nineteen;  int twenty;  int twentyone;
            //initialize all variables at 0
            one=0;  two=0;  three=0;  four=0;  five=0;  six=0;  seven=0;  eight=0;
            nine=0;  ten=0;  eleven=0;  twelve=0;  thirteen=0;  fourteen=0;
            fifteen=0;  sixteen=0;  seventeen=0;  eighteen=0;  nineteen=0;
            twenty=0;  twentyone=0;
    //do dice rolling and count each iteration of each possible result - 36 possible??
            for (int i = 0; i < 36000; i++)
              int roll  = (r.nextInt(6) + 1) ;
              int roll2 = (r.nextInt(6) + 1) ;
            if (roll ==1 && roll2 == 1) ++one;
            if ((roll == 1 && roll2 == 2) || (roll2 == 1 && roll == 2)) ++two;
            if ((roll == 1 && roll2 == 3) || (roll2 == 1 && roll == 3)) ++three;
            if ((roll == 1 && roll2 == 4) || (roll2 == 1 && roll == 4)) ++four;
            if ((roll == 1 && roll2 == 5) || (roll2 == 1 && roll == 5)) ++five;
            if ((roll == 1 && roll2 == 6) || (roll2 == 1 && roll == 6)) ++six;
            if (roll == 2 && roll2 == 2) ++seven;
            if ((roll == 2 && roll2 == 3) || (roll2 == 2 && roll == 3)) ++eight;
            if ((roll == 2 && roll2 == 4) || (roll2 == 2 && roll == 4)) ++nine;
            if ((roll == 2 && roll2 == 5) || (roll2 == 2 && roll == 5)) ++ten;
            if ((roll == 2 && roll2 == 6) || (roll2 == 2 && roll == 6)) ++eleven;
            if ((roll == 3 && roll2 == 3)) ++twelve;
            if ((roll == 3 && roll2 == 4) || (roll2 == 3 && roll == 4)) ++thirteen;
            if ((roll == 3 && roll2 == 5) || (roll2 == 3 && roll == 5)) ++fourteen;
            if ((roll == 3 && roll2 == 6) || (roll2 == 3 && roll == 6)) ++fifteen;
            if (roll == 4 && roll2 == 4) ++sixteen;
            if ((roll == 4 && roll2 == 5) || (roll2 == 4 && roll == 5)) ++seventeen;
            if ((roll == 4 && roll2 == 6) || (roll2 == 4 && roll == 6)) ++eighteen;
            if (roll == 5 && roll2 == 5) ++nineteen;
            if ((roll == 5 && roll2 == 6) || (roll2 == 5 && roll == 6)) ++twenty;
            if (roll == 6 && roll2 == 6) ++twentyone;
            //assign results to array
            dieResults[0][0]=one;
            dieResults[1][0]=two; dieResults[0][1]=two;
            dieResults[2][0]=three;dieResults[0][2]=three;
            dieResults[3][0]=four;dieResults[0][3]=four;
            dieResults[4][0]=five;dieResults[0][4]=five;
            dieResults[5][0]=six;dieResults[0][5]=six;
            dieResults[1][1]=seven;
            dieResults[2][1]=eight;dieResults[1][2]=eight;dieResults[3][1]=nine;
            dieResults[1][3]=nine;dieResults[4][1]=ten;dieResults[1][4]=ten;
            dieResults[5][1]=eleven;dieResults[1][5]=eleven;
            dieResults[2][2]=twelve;
            dieResults[3][2]=thirteen;dieResults[2][3]=thirteen;
            dieResults[4][2]=fourteen;dieResults[2][4]=fourteen;
            dieResults[5][2]=fifteen;dieResults[2][5]=fifteen;
            dieResults[3][3]=sixteen;
            dieResults[4][3]=seventeen;dieResults[3][4]=seventeen;
            dieResults[5][3]=eighteen;dieResults[3][5]=eighteen;
            dieResults[4][4]=nineteen;
            dieResults[5][4]=twenty;dieResults[4][5]=twenty;
            dieResults[5][5]=twentyone;
            //print results
            for (int j=0; j<dieResults.length; j++){
                System.out.print("\n");
                for (int k=0; k<dieResults.length; k++){
                System.out.print("|"+dieResults[j][k]+"| ");
            // Verify program accuracy and ensure total reported results add up to 36000
    System.out.println("\nValidate: 1's; "+dieResults[0][0]+" 1 & 2; "+dieResults[1][0]+
            dieResults[0][1]+" 4's; "+dieResults[2][0]+dieResults[0][2]+" 5's; "
            +dieResults[3][0]+dieResults[0][3]+" 6's; "+dieResults[4][0]+dieResults[0][4]
            +" 7's; "+dieResults[5][0]+dieResults[0][5]);
    System.out.println("All total rolls add up to " + (dieResults[0][0] + dieResults[0][1] +
             dieResults[0][2] + dieResults[0][3] + dieResults[0][4] + dieResults[0][5]));
    //check lowest/highest numbers entered to verify results are reasonable
    //sort results to obtain lowest and highest number of rolls
        for (int l=0; l<sortingResults.length;l++){
         for (int m=0; m<sortingResults.length;m++){
            sortingResults[l]=dieResults[l][m];
    Arrays.sort(sortingResults);
    //determine and print what roll occurred the least
        if (sortingResults[0] == two)
            System.out.println("The lowest number of rolls for a single result is "
                    + "2's with " + sortingResults[0]);
        if (sortingResults[0] == three)
            System.out.println("The lowest number of rolls for a single result is "
                    + "3's with " + sortingResults[0]);
        if (sortingResults[0] == four)
            System.out.println("The lowest number of rolls for a single result is "
                    + "4's with " + sortingResults[0]);
        if (sortingResults[0] == five)
            System.out.println("The lowest number of rolls for a single result is "
                    + "5's with " + sortingResults[0]);
        if (sortingResults[0] == six)
            System.out.println("The lowest number of rolls for a single result is "
                    + "6's with " + sortingResults[0]);
        if (sortingResults[0] == seven)
            System.out.println("The lowest number of rolls for a single result is "
                    + "7's with " + sortingResults[0]);
        if (sortingResults[0] == eight)
            System.out.println("The lowest number of rolls for a single result is "
                    + "8's with " + sortingResults[0]);
        if (sortingResults[0] == nine)
            System.out.println("The lowest number of rolls for a single result is "
                    + "9's with " + sortingResults[0]);
        if (sortingResults[0] == ten)
            System.out.println("The lowest number of rolls for a single result is "
                    + "10's with " + sortingResults[0]);
        if (sortingResults[0] == eleven)
            System.out.println("The lowest number of rolls for a single result is "
                    + "11's with " + sortingResults[0]);
        if (sortingResults[0] == twelve)
            System.out.println("The lowest number of rolls for a single result is "
                    + "12's with " + sortingResults[0]);
    //determine and print what roll occurred the most
        if (dieResults[5][5] == two)
            System.out.println("The highest number of rolls for a single result is "
                    + "2's with " + dieResults[10]);
        if (dieResults[5][5] == three)
            System.out.println("The highest number of rolls for a single result is "
                    + "3's with " + dieResults[10]);
        if (dieResults[5][5] == four)
            System.out.println("The highest number of rolls for a single result is "
                    + "4's with " + dieResults[10]);
        if (dieResults[5][5] == five)
            System.out.println("The highest number of rolls for a single result is "
                    + "5's with " + dieResults[10]);
        if (dieResults[5][5] == six)
            System.out.println("The highest number of rolls for a single result is "
                    + "6's with " + dieResults[10]);
        if (dieResults[5][5] == seven)
            System.out.println("The highest number of rolls for a single result is "
                    + "7's with " + dieResults[10]);
        if (dieResults[5][5] == eight)
            System.out.println("The highest number of rolls for a single result is "
                    + "8's with " + dieResults[10]);
        if (dieResults[5][5] == nine)
            System.out.println("The highest number of rolls for a single result is "
                    + "9's with " + dieResults[10]);
        if (dieResults[5][5] == ten)
            System.out.println("The lohighestwest number of rolls for a single result is "
                    + "10's with " + dieResults[10]);
        if (dieResults[5][5] == eleven)
            System.out.println("The highest number of rolls for a single result is "
                    + "11's with " + dieResults[10]);
        if (dieResults[5][5] == twelve)
            System.out.println("The highest number of rolls for a single result is "
                    + "12's with " + dieResults[10]);
    }the output I got was this:
    run:
    |988| |2038| |1933| |2008| |2095| |2002|
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    |2038| |965| |1973| |1982| |1985| |2028|
    |1933| |1973| |1034| |1952| |1928| |2055|
    |2008| |1982| |1952| |1010| |1981| |2023|
    |2095| |1985| |1928| |1981| |1041| |1996|
    |2002| |2028| |2055| |2023| |1996| |983| Validate: 1's; 988 1 & 2; 20382038 4's; 19331933 5's; 20082008 6's; 20952095 7's; 20022002
    All total rolls add up to 11064
            at d12.randomDice2D.main(randomDice2D.java:101)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)I can see from the validate line in output that i need to start that line with a \n and change the formula of that println from
    +dieResults[x][y]+dieResults[y][x]+
    to
    +(dieResults[x][y]+dieResults[y][x])+
    I am also aware i have to finish listing all the elements of the array to add to the all total rolls line, but that I can add as I wait for an answer to the errors I don't see the solutions to. The last thing I need help with is how to print those results in a 2-d table format instead of one number per line. Should I change the pair of loops here:
    for (int l=0; l<sortingResults.length;l++){
         for (int m=0; m<sortingResults.length;m++){
            sortingResults[l]=dieResults[l][m];
            }    }to a single loop repeated till the end like this?
    for (int l=0; l<6; l++){
    sortingResults[l]=dieResults[l][0];}
    for (int m=0; m<5; m++){
    sortingResults[(m+6)]=dieResults[m][1];}
    for (int n=0; n<4; n++){
    sortingResults[(n+11)]=dieResults[n][1];}or just take 21 lines (or 36 if i track all results instead of all unique) to declare each element like:
    sortingResults[a]=dieResults[x][y];
    Edited by: Geode on Oct 20, 2010 12:38 AM

    Ok, I have done a lot and thought I had worked out all of the problems. Netbeans had no problems compiling it or running it, and the output was exactly what I expected it to be. Then I decided that the println starting "Validate:" was unneccessary since I printed the table right before it, so I deleted it. Noww Netbeans is telling me it compiles with errors every time i run it, even though it doesn't flag any line as having an error and it still executes exactly as I expect it to. Also, I was not able to find a way to make the three big sets of if's into loops. Any suggestions or ideas on how they could be looped would be most welcome. Also, any ideas on why Netbeans is saying it compiles with errors when it does not give any. I'll give the code and then the output again.
    package d12;
    //import classes to use
    import java.util.Random;
    import java.util.Arrays;
    public class randomDice2D {
         * @param args the command line arguments
        public static void main(String[] args) {
    //bring in new Random object
            Random r = new Random();
    //declare all variables and arrays
            int[][] dieResults; dieResults = new int[6][6];
            int[] sortingResults; sortingResults=new int[36];
            int one; int two; int three; int four; int five; int six; int seven;
            int eight; int nine; int ten; int eleven; int twelve; int thirteen;
            int fourteen; int fifteen; int sixteen; int seventeen; int eighteen;
            int nineteen; int twenty; int twentyone; int twentytwo; int twentythree;
            int twentyfour; int twentyfive; int twentysix; int twentyseven;
            int twentyeight; int twentynine; int thirty; int thirtyone; int thirtytwo;
            int thirtythree; int thirtyfour; int thirtyfive; int thirtysix;
            //initialize all variables at 0
            one=0;two=0;three=0;four=0;five=0;six=0;seven=0;eight=0;nine=0;ten=0;
            eleven=0;twelve=0;thirteen=0;fourteen=0;fifteen=0;sixteen=0;seventeen=0;
            eighteen=0;nineteen=0;twenty=0;twentyone=0;twentytwo=0;twentythree=0;
            twentyfour=0;twentyfive=0;twentysix=0;twentyseven=0;twentyeight=0;
            twentynine=0;thirty=0;thirtyone=0;thirtytwo=0;thirtythree=0;thirtyfour=0;
            thirtyfive=0;thirtysix=0;
    //do dice rolling and count each iteration of each possible result - 36 possible??
            for (int i = 0; i < 36000; i++)
              int roll  = (r.nextInt(6) + 1) ;
              int roll2 = (r.nextInt(6) + 1) ;
            if (roll == 1 && roll2 == 1) ++one;
            if (roll == 1 && roll2 == 2) ++two;
            if (roll == 1 && roll2 == 3) ++three;
            if (roll == 1 && roll2 == 4) ++four;
            if (roll == 1 && roll2 == 5) ++five;
            if (roll == 1 && roll2 == 6) ++six;
            if (roll == 2 && roll2 == 1) ++seven;
            if (roll == 2 && roll2 == 2) ++eight;
            if (roll == 2 && roll2 == 3) ++nine;
            if (roll == 2 && roll2 == 4) ++ten;
            if (roll == 2 && roll2 == 5) ++eleven;
            if (roll == 2 && roll2 == 6) ++twelve;
            if (roll == 3 && roll2 == 1) ++thirteen;
            if (roll == 3 && roll2 == 2) ++fourteen;
            if (roll == 3 && roll2 == 3) ++fifteen;
            if (roll == 3 && roll2 == 4) ++sixteen;
            if (roll == 3 && roll2 == 5) ++seventeen;
            if (roll == 3 && roll2 == 6) ++eighteen;
            if (roll == 4 && roll2 == 1) ++nineteen;
            if (roll == 4 && roll2 == 2) ++twenty;
            if (roll == 4 && roll2 == 3) ++twentyone;
            if (roll == 4 && roll2 == 4) ++twentytwo;
            if (roll == 4 && roll2 == 5) ++twentythree;
            if (roll == 4 && roll2 == 6) ++twentyfour;
            if (roll == 5 && roll2 == 1) ++twentyfive;
            if (roll == 5 && roll2 == 2) ++twentysix;
            if (roll == 5 && roll2 == 3) ++twentyseven;
            if (roll == 5 && roll2 == 4) ++twentyeight;
            if (roll == 5 && roll2 == 5) ++twentynine;
            if (roll == 5 && roll2 == 6) ++thirty;
            if (roll == 6 && roll2 == 1) ++thirtyone;
            if (roll == 6 && roll2 == 2) ++thirtytwo;
            if (roll == 6 && roll2 == 3) ++thirtythree;
            if (roll == 6 && roll2 == 4) ++thirtyfour;
            if (roll == 6 && roll2 == 5) ++thirtyfive;
            if (roll == 6 && roll2 == 6) ++thirtysix;
            //assign results to array
            dieResults[0][0]=one;dieResults[1][0]=seven; dieResults[0][1]=two;
            dieResults[2][0]=thirteen;dieResults[0][2]=three;dieResults[3][0]=nineteen;
            dieResults[0][3]=four;dieResults[4][0]=twentyfive;dieResults[0][4]=five;
            dieResults[5][0]=thirtyone;dieResults[0][5]=six;dieResults[1][1]=eight;
            dieResults[2][1]=fourteen;dieResults[1][2]=nine;dieResults[3][1]=twenty;
            dieResults[1][3]=ten;dieResults[4][1]=twentysix;dieResults[1][4]=eleven;
            dieResults[5][1]=thirtytwo;dieResults[1][5]=twelve;dieResults[2][2]=fifteen;
            dieResults[3][2]=twentyone;dieResults[2][3]=sixteen;dieResults[4][2]=twentyseven;
            dieResults[2][4]=seventeen;dieResults[5][2]=thirtythree;dieResults[2][5]=eighteen;
            dieResults[3][3]=twentytwo;dieResults[4][3]=twentyeight;dieResults[3][4]=twentythree;
            dieResults[5][3]=thirtyfour;dieResults[3][5]=twentyfour;dieResults[4][4]=twentynine;
            dieResults[5][4]=thirtyfive;dieResults[4][5]=thirty;dieResults[5][5]=thirtysix;
            //print results
            System.out.println("     1      2      3      4     5      6");
            for (int j=0; j<dieResults.length; j++){
                System.out.print("\n"+(j+1)+" -");
                for (int k=0; k<dieResults.length; k++){
                System.out.print("|"+dieResults[j][k]+"| ");
            // Verify program accuracy to ensure total results add up to 36000
    System.out.println("\nAll total rolls add up to "+(dieResults[0][0]+dieResults[0][1]+
             dieResults[0][2]+dieResults[0][3]+dieResults[0][4]+dieResults[0][5]+
             dieResults[1][0]+dieResults[1][1]+dieResults[1][2]+dieResults[1][3]+
             dieResults[1][4]+dieResults[1][5]+dieResults[2][0]+dieResults[2][1]+
             dieResults[2][2]+dieResults[2][3]+dieResults[2][4]+dieResults[2][5]+
             dieResults[3][0]+dieResults[3][1]+dieResults[3][2]+dieResults[3][3]+
             dieResults[3][4]+dieResults[3][5]+dieResults[4][0]+dieResults[4][1]+
             dieResults[4][2]+dieResults[4][3]+dieResults[4][4]+dieResults[4][5]+
             dieResults[5][0]+dieResults[5][1]+dieResults[5][2]+dieResults[5][3]+
             dieResults[5][4]+dieResults[5][5]));
    //check lowest/highest numbers entered to verify results are reasonable
    //sort results to obtain lowest and highest number of rolls 
    int z;z=0;
    while (z < 36){
    for (int l=0; l<6;l++){
         for (int m=0; m<6;m++){
            sortingResults[z]=dieResults[l][m];++z;
    Arrays.sort(sortingResults);
    //determine and print what roll occurred the least
        if (sortingResults[0] == one)
            System.out.println("The lowest number of rolls for a single result is "
                    + "1's with " + sortingResults[0]);
        if (sortingResults[0] == two)
            System.out.println("The lowest number of rolls for a single result is "
                    + "1 & 2 with " + sortingResults[0]);
        if (sortingResults[0] == three)
            System.out.println("The lowest number of rolls for a single result is "
                    + "1 & 3 with " + sortingResults[0]);
        if (sortingResults[0] == four)
            System.out.println("The lowest number of rolls for a single result is "
                    + "1 & 4 with " + sortingResults[0]);
        if (sortingResults[0] == five)
            System.out.println("The lowest number of rolls for a single result is "
                    + "1 & 5 with " + sortingResults[0]);
        if (sortingResults[0] == six)
            System.out.println("The lowest number of rolls for a single result is "
                    + "1 & 6 with " + sortingResults[0]);
        if (sortingResults[0] == seven)
            System.out.println("The lowest number of rolls for a single result is "
                    + "2 & 1 with " + sortingResults[0]);
        if (sortingResults[0] == eight)
            System.out.println("The lowest number of rolls for a single result is "
                    + "2 & 2 with " + sortingResults[0]);
        if (sortingResults[0] == nine)
            System.out.println("The lowest number of rolls for a single result is "
                    + "2 & 3 with " + sortingResults[0]);
        if (sortingResults[0] == ten)
            System.out.println("The lowest number of rolls for a single result is "
                    + "2 & 4 with " + sortingResults[0]);
        if (sortingResults[0] == eleven)
            System.out.println("The lowest number of rolls for a single result is "
                    + "2 & 5 with " + sortingResults[0]);
        if (sortingResults[0] == twelve)
            System.out.println("The lowest number of rolls for a single result is "
                    + "2 & 6 with " + sortingResults[0]);
        if (sortingResults[0] == thirteen)
            System.out.println("The lowest number of rolls for a single result is "
                    + "3 & 1 with " + sortingResults[0]);
        if (sortingResults[0] == fourteen)
            System.out.println("The lowest number of rolls for a single result is "
                    + "3 & 2 with " + sortingResults[0]);
        if (sortingResults[0] == fifteen)
            System.out.println("The lowest number of rolls for a single result is "
                    + "3 & 3 with " + sortingResults[0]);
        if (sortingResults[0] == sixteen)
            System.out.println("The lowest number of rolls for a single result is "
                    + "3 & 4 with " + sortingResults[0]);
        if (sortingResults[0] == seventeen)
            System.out.println("The lowest number of rolls for a single result is "
                    + "3 & 5 with " + sortingResults[0]);
        if (sortingResults[0] == eighteen)
            System.out.println("The lowest number of rolls for a single result is "
                    + "3 & 6 with " + sortingResults[0]);
        if (sortingResults[0] == nineteen)
            System.out.println("The lowest number of rolls for a single result is "
                    + "4 & 1 with " + sortingResults[0]);
        if (sortingResults[0] == twenty)
            System.out.println("The lowest number of rolls for a single result is "
                    + "4 & 2 with " + sortingResults[0]);
        if (sortingResults[0] == twentyone)
            System.out.println("The lowest number of rolls for a single result is "
                    + "4 & 3 with " + sortingResults[0]);
        if (sortingResults[0] == twentytwo)
            System.out.println("The lowest number of rolls for a single result is "
                    + "4 & 4 with " + sortingResults[0]);
        if (sortingResults[0] == twentythree)
            System.out.println("The lowest number of rolls for a single result is "
                    + "4 & 5 with " + sortingResults[0]);
        if (sortingResults[0] == twentyfour)
            System.out.println("The lowest number of rolls for a single result is "
                    + "4 & 6 with " + sortingResults[0]);
        if (sortingResults[0] == twentyfive)
            System.out.println("The lowest number of rolls for a single result is "
                    + "5 & 1 with " + sortingResults[0]);
        if (sortingResults[0] == twentysix)
            System.out.println("The lowest number of rolls for a single result is "
                    + "5 & 2 with " + sortingResults[0]);
        if (sortingResults[0] == twentyseven)
            System.out.println("The lowest number of rolls for a single result is "
                    + "5 & 3 with " + sortingResults[0]);
        if (sortingResults[0] == twentyeight)
            System.out.println("The lowest number of rolls for a single result is "
                    + "5 & 4 with " + sortingResults[0]);
        if (sortingResults[0] == twentynine)
            System.out.println("The lowest number of rolls for a single result is "
                    + "5 & 5 with " + sortingResults[0]);
        if (sortingResults[0] == thirty)
            System.out.println("The lowest number of rolls for a single result is "
                    + "5 & 6 with " + sortingResults[0]);
        if (sortingResults[0] == thirtyone)
            System.out.println("The lowest number of rolls for a single result is "
                    + "6 & 1 with " + sortingResults[0]);
        if (sortingResults[0] == thirtytwo)
            System.out.println("The lowest number of rolls for a single result is "
                    + "6 & 2 with " + sortingResults[0]);
        if (sortingResults[0] == thirtythree)
            System.out.println("The lowest number of rolls for a single result is "
                    + "6 & 3 with " + sortingResults[0]);
        if (sortingResults[0] == thirtyfour)
            System.out.println("The lowest number of rolls for a single result is "
                    + "6 & 4 with " + sortingResults[0]);
        if (sortingResults[0] == thirtyfive)
            System.out.println("The lowest number of rolls for a single result is "
                    + "6 & 5 with " + sortingResults[0]);
        if (sortingResults[0] == thirtysix)
            System.out.println("The lowest number of rolls for a single result is "
                    + "6 & 6 with " + sortingResults[0]);
    //determine and print what roll occurred the most
        if (sortingResults[35] == one)
            System.out.println("The highest number of rolls for a single result is "
                    + "1's with " + sortingResults[35]);
        if (sortingResults[35] == two)
            System.out.println("The highest number of rolls for a single result is "
                    + "1 & 2 with " + sortingResults[35]);
        if (sortingResults[35] == three)
            System.out.println("The highest number of rolls for a single result is "
                    + "1 & 3 with " + sortingResults[35]);
        if (sortingResults[35] == four)
            System.out.println("The highest number of rolls for a single result is "
                    + "1 & 4 with " + sortingResults[35]);
        if (sortingResults[35] == five)
            System.out.println("The highest number of rolls for a single result is "
                    + "1 & 5 with " + sortingResults[35]);
        if (sortingResults[35] == six)
            System.out.println("The highest number of rolls for a single result is "
                    + "1 & 6 with " + sortingResults[35]);
        if (sortingResults[35] == seven)
            System.out.println("The highest number of rolls for a single result is "
                    + "2 & 1 with " + sortingResults[35]);
        if (sortingResults[35] == eight)
            System.out.println("The highest number of rolls for a single result is "
                    + "2 & 2 with " + sortingResults[35]);
        if (sortingResults[35] == nine)
            System.out.println("The highest number of rolls for a single result is "
                    + "2 & 3 with " + sortingResults[35]);
        if (sortingResults[35] == ten)
            System.out.println("The highest number of rolls for a single result is "
                    + "2 & 4 with " + sortingResults[35]);
        if (sortingResults[35] == eleven)
            System.out.println("The highest number of rolls for a single result is "
                    + "2 & 5 with " + sortingResults[35]);
        if (sortingResults[35] == twelve)
            System.out.println("The highest number of rolls for a single result is "
                    + "2 & 6 with " + sortingResults[35]);
        if (sortingResults[35] == thirteen)
            System.out.println("The highest number of rolls for a single result is "
                    + "3 & 1 with " + sortingResults[35]);
        if (sortingResults[35] == fourteen)
            System.out.println("The highest number of rolls for a single result is "
                    + "3 & 2 with " + sortingResults[35]);
        if (sortingResults[35] == fifteen)
            System.out.println("The highest number of rolls for a single result is "
                    + "3 & 3 with " + sortingResults[35]);
        if (sortingResults[35] == sixteen)
            System.out.println("The highest number of rolls for a single result is "
                    + "3 & 4 with " + sortingResults[35]);
        if (sortingResults[35] == seventeen)
            System.out.println("The highest number of rolls for a single result is "
                    + "3 & 5 with " + sortingResults[35]);
        if (sortingResults[35] == eighteen)
            System.out.println("The highest number of rolls for a single result is "
                    + "3 & 6 with " + sortingResults[35]);
        if (sortingResults[35] == nineteen)
            System.out.println("The highest number of rolls for a single result is "
                    + "4 & 1 with " + sortingResults[35]);
        if (sortingResults[35] == twenty)
            System.out.println("The highest number of rolls for a single result is "
                    + "4 & 2 with " + sortingResults[35]);
        if (sortingResults[35] == twentyone)
            System.out.println("The highest number of rolls for a single result is "
                    + "4 & 3 with " + sortingResults[35]);
        if (sortingResults[35] == twentytwo)
            System.out.println("The highest number of rolls for a single result is "
                    + "4 & 4 with " + sortingResults[35]);
        if (sortingResults[35] == twentythree)
            System.out.println("The highest number of rolls for a single result is "
                    + "4 & 5 with " + sortingResults[35]);
        if (sortingResults[35] == twentyfour)
            System.out.println("The highest number of rolls for a single result is "
                    + "4 & 6 with " + sortingResults[35]);
        if (sortingResults[35] == twentyfive)
            System.out.println("The highest number of rolls for a single result is "
                    + "5 & 1 with " + sortingResults[35]);
        if (sortingResults[35] == twentysix)
            System.out.println("The highest number of rolls for a single result is "
                    + "5 & 2 with " + sortingResults[35]);
        if (sortingResults[35] == twentyseven)
            System.out.println("The highest number of rolls for a single result is "
                    + "5 & 3 with " + sortingResults[35]);
        if (sortingResults[35] == twentyeight)
            System.out.println("The highest number of rolls for a single result is "
                    + "5 & 4 with " + sortingResults[35]);
        if (sortingResults[35] == twentynine)
            System.out.println("The highest number of rolls for a single result is "
                    + "5 & 5 with " + sortingResults[35]);
        if (sortingResults[35] == thirty)
            System.out.println("The highest number of rolls for a single result is "
                    + "5 & 6 with " + sortingResults[35]);
        if (sortingResults[35] == thirtyone)
            System.out.println("The highest number of rolls for a single result is "
                    + "6 & 1 with " + sortingResults[35]);
        if (sortingResults[35] == thirtytwo)
            System.out.println("The highest number of rolls for a single result is "
                    + "6 & 2 with " + sortingResults[35]);
        if (sortingResults[35] == thirtythree)
            System.out.println("The highest number of rolls for a single result is "
                    + "6 & 3 with " + sortingResults[35]);
        if (sortingResults[35] == thirtyfour)
            System.out.println("The highest number of rolls for a single result is "
                    + "6 & 4 with " + sortingResults[35]);
        if (sortingResults[35] == thirtyfive)
            System.out.println("The highest number of rolls for a single result is "
                    + "6 & 5 with " + sortingResults[35]);
        if (sortingResults[35] == thirtysix)
            System.out.println("The highest number of rolls for a single result is "
                    + "6 & 6 with " + sortingResults[35]);
    }and here is the output (after clicking 'Run Anyways' on the project compiled with errors message from Netbeans)
    run:
         1      2      3      4     5      6
    1 -|995| |1014| |1076| |976| |1015| |1000|
    2 -|971| |982| |993| |1067| |983| |1011|
    3 -|990| |951| |1019| |968| |1011| |1023|
    4 -|999| |931| |1009| |987| |1020| |1022|
    5 -|969| |954| |997| |970| |1016| |1028|
    6 -|955| |1014| |996| |996| |1075| |1017|
    All total rolls add up to 36000
    The lowest number of rolls for a single result is 4 & 2 with 931
    The highest number of rolls for a single result is 1 & 3 with 1076
    BUILD SUCCESSFUL (total time: 13 seconds)

  • Sorting a two dimensional array

    Hello!
    I have a twodimensional array that consists of 6 other arrays
    (like array[0 to 5][0 to 4]). The [4] of each sub-array is a index
    number that i'd like to use for sorting the array[0][x] to
    array[5][x] according to their array[x][4]-value. How would I do
    that?
    Thanks!

    use a custom sort function. check the sort() method of the
    array class for sample coding.

  • Assigning value to a two-dimensional byte array - problem or not?

    I am facing a really strange issue.
    I have a 2D byte array of 10 rows and a byte array of some audio bytes:
    byte[][] buf = new byte[10][];
    byte[] a = ~ some bytes read from an audio streamWhen I assign like this:
    for (int i=0; i<10; i++) {
        a = ~read some audio bytes // this method properly returns a byte[]
        buf[i] = a;
    }the assignment is not working!!!
    If I use this:
    for (int i=0; i<10; i++) {
        a = ~read some audio bytes
        for (int j=0; j<a.length; j++) {
            buf[i][j] = a[j];
    }or this:
    for (int i=0; i<10; i++) {
        System.arraycopy(a, 0, buf, 0, a.length);
    }everything works fine, which is really odd!!
    I use this type of value assignment for the first time in byte arrays.
    However, I never had the same problem with integers, where the problem does not appear:int[] a = new int[] {1, 2, 3, 4, 5};
    int[][] b = new int[3][5];
    for (int i=0; i<3; i++) {
    b[i] = a;
    // This works fineAnybody has a clue about what's happening?
    Is it a Java issue or a programmers mistake?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    Back again! I'm trying to track down the problem.
    Here is most of my actual code to get a better idea.
    private void test() {
         byte[][] buf1 = new byte[11][];
         byte[][] buf2 = new byte[11][];
         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile);
         byte[] audioBytes = new byte[100];
         int serialNumber = 0;
         while (audioInputStream.read(audioBytes) != -1) {
              if (serialNumber == 10) {
                   serialNumber = 0; // breakpoint here for debugging
              // Fill buf1 -- working
              for (int i=0; i<audioBytes.length; i++) {
                   buf1[serialNumber] = audioBytes[i];
              // Fill buf2 -- not working
              buf2[serialNumber] = new byte[audioBytes.length];
              buf2[serialNumber] = audioBytes;
              serialNumber++;
    }I debugged the program, using a debug point after taking 10 "groups" of byte arrays (audioBytes) from the audio file.
    The result (as also indicated later by the audio output) is this:
    At the debug point the values of buf1's elements change in every loop, while buf2's remain unchanged, always having the initial value of audioBytes!
    It's really strange and annoying. These are the debugging results for the  [first|http://eevoskos.googlepages.com/loop1.jpg] ,  [second|http://eevoskos.googlepages.com/loop2.jpg]  and  [third|http://eevoskos.googlepages.com/loop3.jpg]  loop.
    I really can't see any mistake in the code.
    Could it be a Netbeans 6.1 bug or something?
    The problem appears both with jdk 5 or 6.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Assigning two dimensional array to each other

    Hi All,
    I have two two-dimensional arrays and I want to assign one's second dimension's values as the other's second dimension's values in this way:
            int userCounter = 0;
            for (int userID : mostCommonUsers) {
                reducedTrainingVectorForSimilarity[userCounter] = trainingVectorForSimilarity[userID];
                userCounter++;
            }In the above code, I have an *"ArrayList<Integer> mostCommonUsers"*, and iterate through this. I'm using the values inside this array list, as a first dimension's index number of the *"trainingVectorForSimilarity"*, and assigning all the values in the second dimension to the left-hand-side array's second dimension. Both array has fixed length of second dimension. So, am I right by doing this, or I should make a for-loop to iterate over the individual values on the second dimensions of both arrays?
    Another similar question: In the below code, *"trainingVectorForSimilarity"* is an *"float[][]"*, so basically I assign all the second dimensions' values into a newly created array named *"trainingUserRatings"*. Again, I assumed that if the corresponding first dimesion's second dimension values will be assign without any iteration over each values.
    for (int i = 0; i < trainingVectorForSimilarity.length; i++) {
                float[] trainingUserRatings = trainingVectorForSimilarity;
    similarity[i] = calculateCosineSimilarity(testingVectorForSimilarity, trainingUserRatings);
    Am I right for both cases?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    Here is the output for the code that you sent:
    [[1, 2, 3], [4, 5, 6]]
    [[1, 2, 3], [4, 5, 6]]
    [[1, 2, 3], [4, 5, 6]]
    [[1, 2, 3], [4, 5, 6]]
    [[1, 2, 3], [4, 999999, 6]]
    [[1, 2, 3], [4, 999999, 6]]
    [[1, 2, 3], [4, 999999, 6]]
    [[1, 2, 3], [4, 5, 6]]
    [null, [4, 999999, 6]]
    [null, [4, 999999, 6]]
    [[1, 2, 3], [4, 999999, 6]]
    [[1, 2, 3], [4, 5, 6]]I confused that even making an array copy still refers the same object but not a copy of that object. For example:
    float[][] trainingVectorForSimilarity is defined and initialized.
                float[] trainingUserRatings = new float[trainingVectorForSimilarity.length];
    trainingUserRatings = trainingVectorForSimilarity[i];
    is the same withfloat[] trainingUserRatings = new float[trainingVectorForSimilarity[i].length];
    System.arraycopy(trainingVectorForSimilarity[i], 0, trainingUserRatings, 0, trainingUserRatings.length);

  • How to get number of rows and columns in a two dimensional array ?

    Hello,
    What would be the simplest way to get number of rows and columns in a two dimensional array represented as integers ?
    I'm looking for another solution as For...Each loop in case of large arrays.
    Regards,
    Petri

    Hi Petri,
    See a attached txt file for obtaining two arrays with upper and lower index values
    Regards
    Ray
    Regards
    Ray Farmer
    Attachments:
    Get2DArrayIndex.txt ‏2 KB

  • How to copy data in text file into two-dimensional arrays?

    Greeting. Can somebody teach me how to copy the input file into two-dimensional arrays? I'm stuck in making a matrix with number ROWS and COLUMNS according to the data in "input.txt"
    import java.io.*;
    import java.util.*;
    public class array
        public static void main (String[] args) throws FileNotFoundException
        { Scanner sc = new Scanner (new FileReader("input.txt"));
            PrintWriter outfile = new PrintWriter("output.txt");
        int[][]matrix = new int[ROWS][COLUMNS];
    }my input.txt :
    a,b,c
    2,2,1
    1,1,1
    2,2,1
    3,3,1
    4,4,1
    5,5,1
    1,6,2
    2,7,2
    3,8,2
    4,9,2
    5,10,2

    import java.io.*;
    import java.util.*;
    public class array {
        public static void main(String[] args) throws IOException {
            FileInputStream in = null;
            FileOutputStream out = null;
    try {
        in = new FileInputStream("input.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = null;
        while ((line = reader.readLine()) != null) {
            String split[]=line.split(",");
    catch (IOException x) {
        System.err.println(x);
    } finally {
        if (in != null) in.close();
    }}}What after this?

  • How to use two dimensional array in custom.pll

    HI
    How to use two dimensional arrays in custom.pll
    I tried by the following way .
    type ship_array is table of number index by binary_integer;
    type vc_array is table of ship_array index by binary_integer;
    But I am getting the error as that
    A plsql table may not contain a table or a record with composite fields.
    Please tell me the way how to use them

    which forms version are you using? two dimensional arrays are available in >= 9i if memory serves.
    regards

  • Variable number of two dimensional arrays into one big array

    I have a variable number of two dimensional arrays.
    The first dimension is variable, the second dimension is always 7.
    i.e.:
    Object[][] array0 = {
    {"tim", "sanchez", 34, 175.5, "bla", "blub", "[email protected]"},
    {"alice", "smith", 42, 160.0, "la", "bub", "[email protected]"},
    Object[][] array1 = {
    {"john", "sdfs", 34, 15.5, "a", "bl", "[email protected]"},
    {"joe", "smith", 42, 16.0, "a", "bub", "[email protected]"},
    Object[][] arrayi = ...I'm generating these arrays with a for-loop:
         for (int i = 0; i < filter.length; i++) {
              MyClass c = new MyClass(filter);
              //data = c.getData();
    Where "filter" is another array which is filled with information that tells "MyClass" how to fill the arrays.
    "getData()" gives back one of the i number of arrays.
    Now I just need to have everything in one big two dimensional array.
    i.e.:Object[][] arrayComplete = {
    {"tim", "sanchez", 34, 175.5, "bla", "blub", "[email protected]"},
    {"alice", "smith", 42, 160.0, "la", "bub", "[email protected]"},
    {"john", "sdfs", 34, 15.5, "a", "bl", "[email protected]"},
    {"joe", "smith", 42, 16.0, "a", "bub", "[email protected]"},
    Any idea on how to accomplish this? It's blowing my mind right now.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    Just brainstorming here:
    Why not put your actual data in a class and store that in a LinkedList (so you know the total number of elements for your multi-dimensional array). Then initalize your multi-dimensional array and populate it? Haven't tested the following, but thinking something along the lines of
    public class MyData {
         //data here
         public Object[] toArray() {
              //something similar to this
              return new Object[] = {"tim", "sanchez", 34, 175.5, "bla", "blub", "[email protected]"};
    LinkedList<MyData> data = new LinkedList<MyData>();
    data.add(mydata1);
    //however many times you need
    Object[][] arrayComplete = new Object[data.size()][7];
    for(int i = 0; i < data.size(); i++) {
         arrayComplete[i] = data.removeFirst().toArray();
    }Another option for knowing how many rows you would need might be using something like:
    int rows = 0;
    rows += array1.length;
    rows += array2.length;
    //etc...But is not entirely useful if you don't know how many variable arrays you have (although reflections might help if the name of the array variable is incremented systematically, someone told me earlier to avoid reflections since it could make debugging a LOT more difficult).
    Edited by: bogdana on Apr 1, 2010 10:38 AM

  • Retrieving  two dimensional arrays from a text file.

    Good evening
    I am having a problem with reading a text file, and putting this info into a 2D array to be put into a table.
    I have the table already(but no code to add rows to the table incase the array increases)
    The array is called Product[a][ b ]
    where a is the product number(the row), and b are details about this product.
    The file i want to be read looks like this:
    data.txt
    CPU|AMD|X2, 64bit, 2GHz|$150|9
    Video card|NVidia|7800 GTX 256MB|$400|4
    e.g. product[2][4] would equal 4
    Basically: Type, manufacturer, specifications, price, amount in stock.
    the "splitter" or "field terminator" is "|"
    This is what i want it to do:
    Store fileline into single string,
    split string and put it in product[ j ][ i ]
    repeat with next lines until nothing is read anymore.
    This is the code I have so far:
    try{
            FileReader textFileReader = new FileReader("C://BACKUP2//data.txt");
            BufferedReader textReader = new BufferedReader(textFileReader);
            for(int j=0; j<0; j++){
                 for(int i=0; i<6; i++){ //i starts at 0, condition: i is under 6, 1 is added to i at each loop/update.             
                       ProductT = textReader.readLine();
                       textReader.close();
                       for(String Product[j] : ProductT.split("|")){ //this line is completly wrong, but i don't know how else to do it.
         // for counting for the number of columns
    catch (IOException e) {System.out.println(e);}
    I don't even think this code makes any bloody sense, I don't know if I'm approaching it correctly.
    Would anyone be gracious enough to give me a helping hand and tell me what/how to change my code?
    from what I see, the problem lies less in the loops, but more in the splitting of the string. no function seems to like 2D arrays :(

    So this is where I stand:
         //Reading
        try{
            FileReader textFileReader = new FileReader("C://BACKUP2//data.txt");
            BufferedReader textReader = new BufferedReader(textFileReader);
            for(int j=0; j>0; j++){ // this will go on forever now or what?
    // yes, this will loop forever, which is fine as long as you break out of the loop when you reach the end of the file (which is indicated by textReader.readLine() returning null)
                 // replace the for(String currentField : ProductT.split("|")) loop with this one
                 //for(int i=0; i<6; i++){ //i starts at 0, condition: i is under 6, 1 is added to i at update.       
                      String inputline = textReader.readLine();    // read a single line from the file each time through the loop
                      //while (inputline != null) {
                            if (inputline != null) {    // check if the end of the file has been reached
                           //ProductT = textReader.readLine(); //put to single string
                       //textReader.close(); //shut it off    // move this line to the end of processing, you don't want to close the file until you've read everything from it.
                       //for(String currentField : ProductT.split("|")){   //Split string...      according to JBuilder "The local variable currentField is never read"
                             // switch this loop to use the regular for(  ;  ;  ) type, that way you will have another counter to use as the second index in the product assignment
                                         //currentField = Product[j];
                        product[i][] = currentField; // use the index of the second loop as the second index here
    else {
    break; // break out of the loop when end of file is reached
         //} // the for i etc. end here
    } // End for j et cetera...
         } //end Try
    catch (IOException e) {System.out.println(e);}
    //cut the crap
    // close the reader here
    ok - so I am not quite sure about what you mean here
    "Assign each of the 5 fields to the five [][j]
    positions of the array. "
    should i put 5 "fors" in there? - currently the
    program is compiling - although they do not appear in
    the table.I put comments in the code that should answer those questions.
    The code for the table is
    Object[][] data = {
              {Product[0][0], Product[0][1],
                   Product[0][2], Product[0][3], Product[0][4]},
                   {Product[1][0], Product[1][1],
    Product[1][2], Product[1][3],
    uct[1][3], Product[1][4]},
                          {Product[2][0], Product[2][1],
    Product[2][2], Product[2][3],
    ct[2][3], Product[2][4]},
                              {Product[3][0], Product[3][1],
    Product[3][2],
    Product[3][2], Product[3][3], Product[3][4]},
                    {Product[4][0], Product[4][1],
    Product[4][2],
    Product[4][2], Product[4][3], Product[4][4]},
    ;If I do not declare any of these strings (e.g. I have
    only declared Product[0-2][] oldschol style) an error
    occurs, telling me that
    Caused by:
    java.lang.ArrayIndexOutOfBoundsException: 3
         at data.<init>(data.java:112)
         ... 5 more
    When you declare product, you'll need to give it a size for both dimensions. The first dimension will be the number of lines to be read from the file. You'll need to know this before allocating the array, so either count them in the file, (if that's good enough for this assignment) or loop through the file once before reading it to count the lines. The second dimension of the array will be 5, since that's how many fields each line has.
    btw; do I give those duke stars when all problems of
    my life(currently: reading the two dimensional
    string) have been resolved?I don't know about [i]all the problems of your life, but when the topic of the thread has been solved, then give them to whoever helped. :)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Problem returning a two-dimensional array in web service

    Hello. I'm having problems returning a two dimensional array in my web service. The service returns a MyClass[][] correctly, but the client receives a different one.
    I've done a test that returns a MyClass[1][1] an the client shows a MyClass[1][20]. All the MyClass objects returned in the array are the same and all it's fields are null. There's even an element of the array containing null instead of the object. As I say, the service creates the array ok, but the client gets other thing.
    I have other methods returning one-dimensional arrays MyClass[] and I have no problem.
    My system:
    WindowsXP
    Tomcat 5.5 (Axis 1.3?)
    JDK 1.5
    Eclipse 3.3
    My wsdl is generated with eclipse, although I've had to update my wsdd manually.
    Any Ideas?
    Thanks.

    Does it have to be stored in an array?
    Because you could use the java.awt.Point class, and a
    java.util.Set to create random points until you have
    the correct number of unique points.Of course it is no must to store it in an array. it was just my first thought of approaching the problem. I will try the Point.class. Thanks for that hint.

Maybe you are looking for

  • FUnction Module For OPen Reservation

    Dear All, CAn you give me details of a fuction module which will return me details of open reservation items. deepak

  • Reg. IDOC's

    Hi All Can any please help me in getting the TCODE(the tcodes other than WE81, WE82) for viewing all the Standard IDOC's provided by SAP and the corresponding segments of those IDOC Types. Thanks in advance ... Vijay.

  • Weblogic 81. portal server doesnot startup after the AM Agent installation

    Hi, I have a problem starting the weblogic server after I create the agent Authentication provider. I have cross checked the CLASSPATH values and the JAVA_OPTIONS values.. it is correct I get the following error weblogic.security.service.SecurityServ

  • How to compile mixed encoded Java source code?

    Hi, everyone, I have .java files, one (file1.java) is encoded ASCII, the other (file2.java) is encoded Unicode. I can compile each file by using javac file1.java javac -encoding Unicode file2.java But I'd like to compile them together in one time. Is

  • Hi i am trying to convert my psd file to jpeg .

    i have photoshop 6 its not giving me the option for jpeg