Dan an array. Determine the amount of maximum elements in the array
The first line sets the number of array elements (no more than 100)
In the second line, the elements of the array are introduced.
I know how to find the maximum value in the array with random numbers, but how to stuff in an array elements from the keyboard (as in the task), if they are written in the string via the space? And how to work with them?
1, Authority 100%
I will answer the question “how to fill the array by the elements entered from the keyboard.”
To do this, we need Scanner
.
Public Static Void Main (String [] Args) {
Scanner Input = New Scanner (System.in); // declare Scanner
System.out.PrintLN ("ENTER Array Length:");
int size = input.nextint (); // Read the size of the array and write in size from the keyboard
INT ARRAY [] = NEW INT [SIZE]; // Create an INT array size in size
System.Out.printLN ("Insert Array Elements:");
/ * Walk throughout the massif, filling it * /
For (int i = 0; i & lt; size; i ++) {
Array [i] = input.nextint (); // Fill the array by elements entered from the keyboard
}
System.Out.print ("Inserted Array Elements:");
For (int i = 0; i & lt; size; i ++) {
System.out.print ("" + Array [i]); // Display the resulting array
}
System.Out.printLN ();
}
And then work with an array just as worked earlier with an array filled with random numbers.
2, AUTHORITY 11%
But for ArrayList
, processed a bit, try to add using Nameoflist.add (Something)
Public Static Void Main (String [] Args) {
Scanner Input = New Scanner (System.in); // declare Scanner
System.out.PrintLN ("ENTER Array Length:");
int size = input.nextint (); // Read the size of the array and write in size from the keyboard
List & lt; Integer & gt; Subarray2 = new arraylist & lt; & gt; (); // Create an INT array size in size
System.Out.printLN ("Insert Array Elements:");
/ * Walk throughout the massif, filling it * /
For (int i = 0; i & lt; size; i ++) {
Subarray2.add (input.nextint ()); // Fill the array by elements entered from the keyboard
}
System.Out.print ("Inserted Array Elements:");
System.out.printLN (subarray2);
}
3
So, it seems beautiful!?
Public Static Void Main (String [] AGRS)
{
int [] a = new int [7];
int i = 0;
Scanner in = New Scanner (System.in);
For (Int Element: a)
{
System.out.print ("Enter" + (i + 1) + "-d array element:");
a [i] = in.nextint ();
System.out.printLN (A [I] + "-");
I ++;
}
For (Int Element: a)
System.Out.print (Element + ");
}
or so:
Public Static Void Main (String [] AGRS)
{
int [] a = new int [7];
int i = 0;
Scanner in = New Scanner (System.in);
For (Int Element: a)
{
System.out.print ("Enter" + (i + 1) + "-d array element:");
a [i] = in.nextint ();
System.out.printLN (A [I] + "-");
I ++;
}
System.out.PrintLN (arrays.tostring (a));
}