CodeGym /Java Course /Java Syntax Zero /Examples of using arrays

Examples of using arrays

Java Syntax Zero
Level 6 , Lesson 3
Available

1. Useful examples of working with arrays:

I think the time has come to move on to some practical tasks. We'll start with a few of the simplest:

Filling an array of 10 numbers with the numbers from 0 to 9:
int[] array = new int[10];
for (int i = 0; i < 10; i++) {
   array[i] = i;
}
  • Create a 10-element array object
  • Loop from 0 to 9 (inclusive)
  • Assign the values from 0 to 9 to the cells
Filling an array of 10 numbers with the numbers from 1 to 10:
int[] array = new int[10];
for (int i = 0; i < 10; i++) {
   array[i] = i + 1;
}
  • Create a 10-element array object
  • Loop from 0 to 9 (inclusive)
  • Assign the values from 1 to 10 to the cells
Filling an array of 10 numbers with the numbers from 10 to 1:
int[] array = new int[10];
for (int i = 0; i < 10; i++) {
   array[i] = 10 - i;
}
  • Create a 10-element array object
  • Loop from 0 to 9 (inclusive)
  • Assign the values from 10 to 1 to the cells

2. Displaying numbers in reverse order

Now let's move on to more complex and interesting examples. We'll start with the following task: read 10 numbers from the keyboard and display them in reverse order.

We know how to read numbers from the keyboard. But how do we read 10 numbers? We could, of course, create 10 variables: a1, a2, etc. But that would be super inconvenient. And what if we had to read in 100 numbers? Would we create 100 variables? As it happens, we just learned about arrays, which are created to store lots of values.

The code for reading in 10 values would look something like this (this snippet would appear inside the main method):

Scanner console = new Scanner(System.in);
int[] array = new int[10];
for (int i = 0; i < 10; i++) {
   array[i] = console.nextInt();
}
  • Create a Scanner object
  • Create a 10-element array object
  • Loop from 0 to 9 (inclusive)
  • Read a number from the keyboard and save it in the next cell of the array

But how do you print the values of the array in reverse order?

To do this, we need one more loop, where i will take values from 9 to 0 (don't forget that the number of array indices starts from 0). The final program code will look something like this:

Scanner console = new Scanner(System.in);
int[] array = new int[10];
for (int i = 0; i < 10; i++) {
   array[i] = console.nextInt();
}
for (int i = 9; i >= 0; i--) {
   System.out.println(array[i]);
}
  • Create a Scanner object
  • Create a 10-element array object
  • Loop from 0 to 9 (inclusive)
  • Read a number from the keyboard and save it in the next cell of the array
  • Loop from 9 to 0 (inclusive)
  • Display the next cell in the array

6
Task
New Java Syntax, level 6, lesson 3
Locked
String array in reverse order
1. Create an array of 10 strings. 2. Enter 8 strings from the keyboard and save them in the array. 3. Display the contents of the entire array (10 elements) on the screen in reverse order. Each element on a new line.

3. Finding the minimum element in an array

Let's take a look at a very interesting and common task: finding the minimum element in an array. We'll grab the code we used to populate the array in the previous task:

Scanner console = new Scanner(System.in);
int[] array = new int[10];
for (int i = 0; i < 10; i++) {
    array[i] = console.nextInt();
}
  • Create a Scanner object
  • Create a 10-element array object
  • Loop from 0 to 9 (inclusive)
  • Read a number from the keyboard and save it in the next cell of the array

Now all we need to do is write code that will find the minimum element in the array and display it on the screen. How do you do that?

Well, to find the minimum element, you need to:

  • Take the array's first element as the "current minimum".
  • Compare all the elements of the array with it one by one
  • If the next element is less than the "current minimum", then update the value of the "current minimum"

This is how it will look in code:

Scanner console = new Scanner(System.in);
int[] array = new int[10];
for (int i = 0; i < 10; i++) {
    array[i] = console.nextInt();
}
int min = array[0];

for (int i = 1; i < 10; i++) {
    if (array[i] < min)

       min = array[i];
}
System.out.println(min);
  • Create a Scanner object
  • Create a 10-element array object
  • Loop from 0 to 9 (inclusive)
  • Read a number from the keyboard and save it in the next cell of the array
  • The zeroth element of the array is taken as the minimum number
  • Loop from 1 to 9 (inclusive)
  • If the current element is less than the "current minimum"
  • then update the value of the "current minimum"
  • Display the found minimum number on the screen

6
Task
New Java Syntax, level 6, lesson 3
Locked
Array of numbers in reverse order
1. Create an array of 10 numbers. 2. Enter 10 numbers from the keyboard and write them to the array. 3. Display the elements of the array in reverse order. Display each value on a new line.
Comments (28)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
AngelN Level 6, Manila, Philippines
2 July 2024
Anyone completed the Reverse task? I.AM.LOST.
Evgeniia Shabaeva Level 32, Budapest, Hungary
21 March 2024
In the 'find the minimum' task I: 1) read a number N from the keyboard: Scanner console = new Scanner(System.in); int N = console.nextInt(); 2) initialized an array array of N elements: int[] array = new int[N]; 3) filled it in with the numbers read from the keyboard: for (int i = 0; i < array.length; i++) { array[i] = console.nextInt(); } Why is the first requirement still not met? The second one is fine.
Evgeniia Shabaeva Level 32, Budapest, Hungary
21 March 2024
Well, I've downloaded the solution. It suggests that we read a number N from the console this way: int n = Integer.parseInt(scanner.nextLine()); Why should we do it this strange and complicated way (scanning the input information as a string and then changing it into an integer) if we could just read the input info as an integer immediately (i.e. by using the nextInt() method)? Why not give a hint in the task conditions that the input will be given as a string?
Abhijeet Level 9, United States
23 March 2024
First requirement didn't meet because instead of using the provided class variable 'array', you created a new local variable 'int[] array'. I ran into the same issue.
haalk3n Level 6, Romania
15 February 2024
The Reverse exercise makes absolutely 0 SENSE! The requirements don't make sense, the solution doesn't make sense!!! You have 6 numbers for input, but only an array of 5 elements ????????????????? 1.Reads an integer N from the console. 2. If the number N is greater than 0, then the program reads an additional N integers. 3.Print the numbers to the console. If N is odd, display them in the order in which there were entered. Otherwise, display them in reverse order. It says CLEARLY "PRINT THE NUMBERS TO THE CONSOLE", so all 6 numbers, ye? We lose the initial input (number 5)... WHY? I've been trying for god damn hours to make this work with all 6 numbers. It should say on 2. If FIRST N is greater than 0, program reads an additional N ints AND ONLY COUNT THOSE ADDITIONAL ONES. I PAY MONEY FOR THIS.
haalk3n Level 6, Romania
15 February 2024
The last 2 exercises, super clear what to do, easily done in 1 go even if I wrote a lot of code for them. Seamlessly implemented the solution.
19 February 2024
You copied the first 3 points from the task, but it continues like this "Display each number on a new line. Do not display the number N. Requirements: • The program should read a number N, followed by N numbers."... It would make more sense if we had to ask the app user with a text like: "How many numbers would you like to input?" that will be the first 5 then "Please type the " + 5 + "numbers (press enter after eachs)"...
Mark Palmer Level 6, Brooklyn, United States
2 February 2024
why are the solutions using .parseInt? aren't the user input's already int type?? i dont follow why you have to convert...
Desiree Wilson Level 8, United States of America, USA Expert
27 February 2024
Unfortunately, I agree. I question how the tests are written at this point now. I stopped caring about the "points" now. I'm more focused on learning the information. I don't see how my answer is not correct but hey, I have other things to think about.
Parsa Level 68, Bangalore, India Expert
14 October 2023
/* Comment has been deleted */
Abhishek Tripathi Level 72, Rewa, India Expert
22 December 2023
Bro anything in java will only run when it is either :- 1. inside the main method or 2. inside the main method you call it.
Parsa Level 68, Bangalore, India Expert
22 December 2023
Exactly my point. Why even mention it.
Abhishek Tripathi Level 72, Rewa, India Expert
24 December 2023
😶😀
Campbell, Jackson Level 12, Corona, USA
11 October 2023
Actually super easy, arrays are my jam :)
Acuna, Landon Level 6, United States of America, USA
5 December 2023
You only speak facts fr !
Nguyen, Louis Level 10, Corona, USA
31 August 2023
Do not declare a type for array; array is declared on line 10 took me 13 attempts to finally look at the screen properly
Richard Loera Level 6, France, France
30 November 2023
Thanks
msosinski Level 18, Poland, Poland
27 January 2023
For maximum task one line of difference - same result, not met requirements.
msosinski Level 18, Poland, Poland
27 January 2023
For minimum task - sounds too ambiguous. It wasn't stated that numbers will appear as Strings instead of integers.
Pekotski Level 16, Zurich, Switzerland
3 July 2023
Exactly.
Mark Palmer Level 6, Brooklyn, United States
2 February 2024
what he said.... where does it indicate that the user input (in this case only numbers....) should be received as anything other than int type??
Pinapleu Level 11, Romania
28 September 2024
Exactly, there was no way to know that was needed
Gummy C Level 11, United States of America, United States
29 December 2022
Man I am so confused about this assignment. I am not sure what it is asking me to do....Also why are we taking in a String and then converting it?