-1

I would like to print the entire array sPack[]. I take in up to 500 values into sPack then I sort in ascending order. My median value is off so I would like to print what my sort method is doing so I can see if it is an issue with my median method or my sort method.

int [] sPack = new int [500];
int i = 0;
String input = br.readLine();
while(input!=null && i<500 && input.length()!=0)//(!input.isEmpty()&& i<500)//
{
  sPack [i] = Integer.parseInt (input);
  i++;
  input = br.readLine();
}
sort(sPack, i);
double Var = calcVariance(sPack,i);
System.out.println (sPack); //this is where i need help***************<==
Jack A.
  • 4,245
  • 1
  • 20
  • 34

3 Answers3

2

Use java.util.Arrays.toString() this is the easiest and convinient method to print all elements of an array.

System.out.println(Arrays.toString(sPack));

Or you can choose the hard way to traverse through the array and print each element.

for(int i =0; i < sPack.length; i++){
    System.out.println(sPack[i]);
}

Great, if you are using Java 8

Stream.of(strArray).forEach(System.out::println);

Have a good day :)

Dave Ranjan
  • 2,966
  • 24
  • 55
1

Try this

for (int pack : sPack) {
    System.out.println(pack);
};
xlm
  • 6,854
  • 14
  • 53
  • 55
HTMLlama
  • 1,464
  • 2
  • 11
  • 16
0

Apart from the other answers provided here, you can also do it the following way:

Arrays.stream(sPack).forEach(System.out::println);

VHS
  • 9,534
  • 3
  • 19
  • 43