DEV Community

aiodell
aiodell

Posted on

Java: Display Simple Arrays

Part 1 of my data structure journey.

This is serving more as personal notes for myself to grasp my understanding of each topic on at least a basic level, but I am also sharing in case there are people who are going through the same learning process as I am!

This is going to be something really simple and is not meant to be any more than creating and looping through an array to display no the console.

All of my code and comments for myself are found in the GitHub repo. My full notes are in the readme file.


Arrays are the most basic data structure people can work with. They are defined by placing square brackets [ ] after the data type, followed by the name of the variable. There are two different ways that I have created an array.

For the examples, I will be creating an array of strings. One way to initialize an array is to state the length of the array like so

String[] names = new String[3];
Enter fullscreen mode Exit fullscreen mode

By doing this, I know my array is going to have a length of 3. I can manually add the information from there and specify which index want to use for which information.

names[0] = "Avery";
names[1] = "Otto";
names[2] = "Emily";
Enter fullscreen mode Exit fullscreen mode

Another way I made an array was to immediately place data inside the array. It has the same length as the other one, but I placed everything in one line.

String[] moreNames = {"Paul", "Fred", "Harold"};
Enter fullscreen mode Exit fullscreen mode

If I had wanted to select a specific element I would have used the array name and index. In this case I wanted to display the entire array.


Display the Array

For displaying the array, I will show two different ways: for-each and for loop. They give the same result, but I want to show the approaches to both.

For-each
Because of the simplicity of the array, I feel like I would personally use this loop to display what I need. The format for the for-each loop is this

for(String name : names){
  System.out.println(name);
}
Enter fullscreen mode Exit fullscreen mode

A way I did to remember the for-each loop was to say "for each name in the names array". The array will print each element on separate lines.

For Loop
The for loop was more tedious but was good to practice for when things become more complex in the future. The format is this:

for(int i = 0; i <= moreNames.length -1; i++){
  System.out.println(name);
}
Enter fullscreen mode Exit fullscreen mode

It will result in the same thing. To explain the loop:

  • The first statement will execute one time before the execution of the code block.

  • The second statement is the condition that determines if the loop ends or continues.

  • The third statement is executed every time after the code block. It will not execute once the condition is no longer true.


Extra: Integers and Array

I thought it would be good to get a feel for iterating through integers rather than strings.

I made this array:

int[] nums = new int[]{1,5,7,8,3,4,0};
Enter fullscreen mode Exit fullscreen mode

and created the for loop like so:

for(int i = 0; i < nums.length; i++){
  nums[i] +=1;
  System.out.print(nums[i] + ", ");
}
Enter fullscreen mode Exit fullscreen mode

I wanted to do something a little extra with my looping by adding 1 to each element before printing. The for loop is the same, but has nums[i] +=1; before the print statement. += 1 is the same as num[i] = i + 1;. so with each loop through the elements, i is increased by 1 and updated within the array.


How about finding the sum of the integers? I will leave that to you! One thing to remember is that the array was already looped through once, and we did add 1 to each integer!

Thank you for your time!

Top comments (0)