0

I am using Java Arrays.sort in order to sort elements of a two dimensional array:

int[][] RAM = new int[4][10000];

I fill the RAM array with integers and then call:

for (i=0;i<4;i++){
    Arrays.sort(RAM[i]);
System.out.println(i);
}

This results in all elements of RAM[][] being filled with zeros. What am I doing wrong?

1
  • 1
    how do you know they are filled with 0? can you show the code segment that you test that? Commented May 17, 2012 at 13:18

3 Answers 3

2

Did you actually fill the Array with numbers first? And if you did, you are only printing out the First part of your 2D array. You need to print out all 40,000 entries. 10,000 for each array in array. So [0][0...9999], [1][0...9999] etc.

Sign up to request clarification or add additional context in comments.

10 Comments

I print the element RAM[3][9997] before sorting and is 5656 and after sorting is 0
Maybe you just need to make your own sort method, or read up on how the sort() method works in Java. Wait, you are only sorting on RAM[i] not RAM[i][j]...might be why, perhaps?
i sort RAM[i] as i need to sort each row of the 2d table. Say for example RAM[0] is {6,5,2,3,4,1} i want to result into {1,2,3,4,5,6}
But do you have values in both your first and second dimension that you need to sort? <_<
Yes, i print the element RAM[3][9997] before sorting and is 5656 and after sorting is 0
|
0
public static void show (int [][] list)
{
    System.out.println ("- show: -");
    for (int [] ia : list) {
        for (int i : ia)
            System.out.print (i + ", ");
        System.out.println ();
    }
    System.out.println ("---");
}

static Random random = new Random ();

public static void main (String [] args)
{
    int[][] RAM = new int[4][2];
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 2; ++j)
        {
            RAM[i][j] = random.nextInt (20);
            System.out.print (i*2 +j);
        }
    }
    show (RAM);
    for (int i=0; i<4; i++) {
        Arrays.sort (RAM[i]);
    }
    show (RAM);
    System.out.println ();
}

Result:

- show: -
8, 13, 
12, 10, 
16, 3, 
7, 1, 
---
- show: -
8, 13, 
10, 12, 
3, 16, 
1, 7, 
---

4 Comments

This doesn't answer the query as to why the sort method is setting zeros in the arrays.
@cst1992: I don't remember this Q&A and what I had in mind. From reading all comments in all answers again, my suspicion is, that instead of just sorting the array, he somehow created a new array, setting this one accidentally to zeros. Maybe I just wanted to show him, how to initialize, sort and view an array by example, so that he could find the error by himself, as long as he doesn't show a fully, self-contained example of his code, which demonstrates the error. My loop for sorting the 4 arrays is identical to his code (except for not printing the index), which disproves the headline.
The main difference between your code and his is that his array is int[4][10000] whereas yours is int[4][2]. The sort method is putting his sorted values at the very end; resulting in the loop printing only zeros - we assume the sort method will keep the excess zeros as they are(which obviously doesn't happen). So we need to pass the indexes as well - Arrays.sort(RAM[i], 0, 2). I can't tell what you had in mind either - but I'd guess you showed him what worked, and maybe he just used your code. That's just a guess, though.
I disagree. Using an int[4][2] array instead of int[4][10000] only makes the sample data easier to oversee. The sort method sorts all values - I don't even know what you're trying to say with "sorted values at the very end". You think there are some unsorted values at the start? Can you write sample code, which shows your claim? For sorting the whole array (all 4 of them) we needn't pass indexes. What are excess zeros?
0

I think the reason is that you didn't set a value for all elements of your array. For the item you didn't initialize, the default value is 0. Thus when you sort the all items in the array, the 0s are swapped to the first part of your array. If you output the first parts of the array, all you see are zeros. I guess so.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.