creating arrays in eclipse
I need to make the random array print, and it does, & then i need to make the code sort my random array and print that. I think, i have missed something out on the code,
Can anyone help me please?
Thanksimport java.util.开发者_C百科ArrayList;
import java.util.Random;
public class Lab5
{
public static void main(String[]args)
{
Random r = new Random();
int[]arr = new int[5];
for(int i=0;i<arr.length;i++)
{
arr[i] = Math.abs(r.nextInt()%255) +1;
System.out.print(arr[i] + "\t");
}
System.out.println();
}
public static void ShowArray(ArrayList<Integer> array) {
for (int i=0; i<array.size(); i++) {
System.out.println(array.get(i));
System.out.println("Sort A: ");
ArrayList<Integer> sortedArrayA = ThreeSorts.SortA(array);
ShowArray(sortedArrayA);
}
}
}
Random r = new Random();
int[]arr = new int[5];
for(int i=0;i<arr.length;i++)
{
arr[i] = Math.abs(r.nextInt()%255) +1;
System.out.print(arr[i] + "\t");
}
System.out.println();
Arrays.sort(arr);
for(int item: arr)
System.out.println(item);
Retrace your execution - does the code that you wrote run?
Don't forget that code in a function only runs when the function is called.
The following code may give you some useful hints:
public static void main(String[] args) {
int[] array = new int[] {3,4,65,1,43};
System.out.println(Arrays.toString(array));
Arrays.sort(array);
System.out.println(Arrays.toString(array));
}
Notes:
I skip the part related to generating random integer, because you have it
to print an array you may use
Arrays.toString()
to sort an array you may use
Arrays.sort()
In method main you must add ShowArray(arg).
As @Mikeb and @Belinda point out, ShowArray is not being called. Furthermore, it is an infinitely recursive function, as it calls itself without a base case to terminate; perhaps you meant to put some of the lines in the main method? I corrected the indentation on your code so as to see this better.
精彩评论