-
Arrays
Hi,
This is a program to sort the numbers in num array. This program does work, but I want to enter new values to the array num every time during execution. How do we give input to an array?
import java.util.*;
public class sort{
public static void main(String[] args){
int num[] = {50,20,45,82,25,63};
int i,j,t;
System.out.println("\n");
System.out.print("Accending order of numbers : " + num[i] );
Arrays.sort(num);
for(i = 0;i <num.lengthl;i++){
System.out.print(" " + num[i]);
}
}
}
Regards,
Sarah
-
Do you mean you want to declare the array like this
Code:
int[] anIntArr = new int[10];
And fill it with random numbers in each spot? If you do, it's pretty simple, after declaring an array as I have done above you can loop through it and assign each item in the array with =.
-
Remember that arrays have a fixed length. Once created (and filled) you cannot add another value to it. You have to create another larger array, copy over all values and then add the new value.
Then again it is not clear what you are trying to do.
-
No, for example the first time I am executing this program I want to enter 50,20,45,82,25,63 as input for sorting into num array and the other time time I execute I want to enter something like 60,46,29,78,39 into num array and when I execute again I want to enter some other numbers
-
Ahhh, in that case do not hard code the values. Either pass in the values as command line arguments (which go to the args array) or get user input.
-
Is it no possible to do in some other way other than passing the values as cmmand line arguments??
-
I do believe I gave 2 options in my previous post.
-
Look up the scanner API, that and a loop can create an array of a user defined size and let you fill it with items.