+ Reply to Thread
Results 1 to 2 of 2

Thread: Array sorting results are all equal to zero

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    3

    Array sorting results are all equal to zero

    I use Java Arrays.sort in order to sort rows of a two dimension array:

    Code:
    int[][] RArray= new int[4][10000];
    I fill RArrayarrays with integers and then:
    Code:
    for (i=0;i<4;i++){
            Arrays.sort(RArray[i]);
            System.out.println(i);
    }
    this results to all rows of RArray[][] being filled with zeros, what am i doing wrong?

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    The right way to sort the Array is as follows :
    Code:
    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[][] RArray = new int[4][2];
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 2; ++j)
            {
                RArray[i][j] = random.nextInt (20);
                System.out.print (i*2 +j);
            }
        }
        show (RArray);
        for (int i=0; i<4; i++) {
            Arrays.sort (RArray[i]);
        }
        show (RArray);
        System.out.println ();
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts