fork download
  1. import java.util.Arrays;
  2.  
  3. class Main{
  4. public static void main (String[] args){
  5. int[] test = {0,1,2,3,4,5};
  6. System.out.print("testing method displayArray1: ");
  7. String array1 = displayArray1(test);
  8. System.out.println(array1);
  9. System.out.print("testing method displayArray2: ");
  10. String array2 = displayArray2(test);
  11. System.out.println(array2);
  12. }
  13. /**
  14. * Purpose: display the specified array in "standard" form, like [0, 1, 2, 3]
  15. * @param arr the array of ints to display
  16. * @return a String containing the array contents in "standard", as shown above.
  17. * You must use a for loop, processing each of the items in arr, to build this String
  18. */
  19. public static String displayArray1(int[ ] arr)
  20. {
  21. String acc = "[";
  22. for (int i = 0; i<arr.length; i++){
  23. acc += arr[i];
  24. if (i<arr.length-1)
  25. acc+=", ";
  26. }
  27. acc += "]";
  28. return acc;
  29. }
  30.  
  31. /**
  32. * Purpose: display the specified array in "standard" form, like [0, 1, 2, 3]
  33. * @param num the array of ints to display
  34. * @return a String containing the array contents in "standard", as shown above.
  35. * You must use the toString method from the Arrays class to build this String
  36. */
  37. public static String displayArray2(int[ ] num)
  38. {
  39. String acc = Arrays.toString(num);
  40. return acc;
  41. }
  42. }
Success #stdin #stdout 0.12s 36320KB
stdin
Standard input is empty
stdout
testing method displayArray1: [0, 1, 2, 3, 4, 5]
testing method displayArray2: [0, 1, 2, 3, 4, 5]