0

I need to sort an array in ascending and descending order and add its value.

Example code:

Integer[] c={3,2,4,1};
Integer[] c1= new Integer[3];

Arrays.sort(c);

c1=c;

System.out.print(c1[0]);

Arrays.sort(c,Collections.reverseOrder());

System.out.print(c1[0]); *\ here I'm getting different result \*
2
  • I can't reproduce your problem: ideone.com/Y6k8U2 Commented Aug 18, 2015 at 16:48
  • To see the full array, change your print statements to System.out.println(Arrays.asList(c1)) Commented Aug 18, 2015 at 17:11

6 Answers 6

2

Bellow program works well

 public class Solution {

static Integer[] c = {3, 2, 4, 1};
static Integer[] c1 = new Integer[3];

public static void main(String arg[]) {
    Arrays.sort(c);
    c1 = c;

    Arrays.sort(c, Collections.reverseOrder());
    System.out.println("  Descending = ");
    for (int i = 0; i < c.length; i++) {
         System.out.println(+ c1[i]);
        }
    Arrays.sort(c);
    System.out.println("Ascending  = " );
    for (int number : c) {
       System.out.println( number);
       }

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

Comments

1

You forgot to add the values of the array up. Here is an answer showing the arrays contents step-by-step using the other suggestions for correct copying of arrays and setting c1 to the right length.

// Prints out array contents and sum
public static void printSumArray( Integer[] iArray){
    int iCumulativeSum = 0;

    for (int i = 0; i < iArray.length; i++)
    {
        System.out.print( " " + iArray[i] + " " );
        iCumulativeSum += iArray[i];
    }
    System.out.println("  Cumulative Sum = " + iCumulativeSum);
}

public static void main(String[] args) {
    Integer[] c = {3,2,4,1,0};
    Integer[] c1 = new Integer[c.length];

    System.out.print("Original Array c:   ");
    printSumArray(c);

    Arrays.sort(c);

    System.out.print("Sorted Array c:     ");
    printSumArray(c);

    System.arraycopy(c, 0, c1, 0, c.length);
    Arrays.sort(c1,Collections.reverseOrder());


    System.out.print("Reversed Sorted c1: ");
    printSumArray(c1);      
}

output:

    Original Array c:    3  2  4  1  0   Cumulative Sum = 10
    Sorted Array c:      0  1  2  3  4   Cumulative Sum = 10
    Reversed Sorted c1:  4  3  2  1  0   Cumulative Sum = 10 
    

1 Comment

Your code could be simplified by replacing 5 print statements with one, using System.out.println("Original Array c: " + Arrays.asList(c))
0

Of course you're getting a different result, you just resorted the one and only array (c1 and c are pointing to the same array).

I think you're confused about what c1=c does. It doesn't copy the array.

Updated

What you mean to do is:

Integer[] c1 = new Integer[c.length]; // note corrected size
System.arraycopy(c, 0, c1, 0, c.length);

1 Comment

Ya u r right! Plz give a solution to copy the sorted array.
0

Change your second line to:

Integer[] c1= new Integer[4];

If it is 3 (instead of 4) then there are only 3 spaces in the array; c1[0], c1[1] and c1[2]

2 Comments

You are right, that's probably what he meant to do, but it really doesn't matter, because the c1=c statement two lines down is discarding that array anyway.
Thats not my actual prblm. Its my typing mistake.
0
package sortadd;
import java.util.*;
public class Sortadd {

    public static void main(String[] args) {
        int temp=0;
      int [] array = {3,2,1};
      int asum=0;
      int dsum=0;
      int sum=0;
      for(int i =0; i<array.length; i++){
          for(int j=i+1;j<array.length; j++){
              if(array[i]>array[j]){
                  temp=array[i];
                  array[i]=array[j];
                  array[j]=temp;
              }
          }
      }
      System.out.println("Ascending");
      for(int i=0; i<array.length; i++){
          System.out.println(array[i]+" ");
          asum +=array[i];
      }
      for(int i =0; i<array.length; i++){
          for(int j=i+1; j<array.length; j++){
              if(array[i]<array[j]){
                  temp=array[i];
                  array[i]=array[j];
                  array[j]=temp;
              }
          }
      }
      System.out.println("Descending");
      for(int i =0; i<array.length; i++){
          System.out.println(array[i]+" ");
          dsum+=array[i];
      }
      //sum of sorted ascending and descendin
      sum =asum+dsum;
      System.out.println("SUM"+sum);
    }

}

Comments

-1

In the line,

Arrays.sort(c,Collections.reverseOrder());

You aren't actually changing the value of the array. Instead, use this line of code to change the array.

c1 = Arrays.sort(c,Collections.reverseOrder());

When you are calling just the method Arrays.sort(...), it returns a new array, but doesn't actually change the array itself. but by changing c1's values to the return of Arrays.sort(...), the array will change.

1 Comment

Arrays.sort does exactly that: "changing the value of the array". Both Arrays.sort() and Collections.sort() replace the content.

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.