0

I'm trying to sort an int array with the Arrays.sort method, but it deletes everything in my array and replaces it with 0. What is happening here? It's printing the correct numbers the first time around, but after I sort it, it has replaced everything with 0. It runs through the loop a couple of times, and each time I get more and more numbers in the array, but it's never the correct ones.

while(rs.next()){
    amount[0] = rs.getInt("ramamount");
    amount[1] = rs.getInt("casesamount");
    amount[2] = rs.getInt("mainboardamount");
    amount[3] = rs.getInt("cpuamount");
    amount[4] = rs.getInt("gfxamount");
    String nome = rs.getString("nome");
    System.out.println(nome + ": RAM=" + amount[0] + " CASES=" + amount[1] + " MAINBOARD=" + amount[2] + " CPU=" + amount[3] + " GFX=" + amount[0]);
    Arrays.sort(amount);
    System.out.println(amount[0] + ", " + amount[1]+ ", " + amount[2]+ ", " + amount[3]+ ", " + amount[4]);
}
1
  • 2
    Arrays.sort is being called in the code in the question Commented Mar 19, 2015 at 21:43

1 Answer 1

4

Make sure your amount array has length 5 otherwise sort will put zeros as first elements and move your values to higher indices.

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

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.