Hey, welcome back to my little ramblings about coding. Like I said at the end of the last article, this article will be about combination operators, repetition, arrays, 2D arrays, and array lists. I’ll be showing all my code in Java through Visual Studio code. Without wasting any time, let’s get right into it.
Wouldn’t it be convenient in coding if there were symbols you could type to represent the word “and” or the word “or”? Well, the creators of Java thought so too so they ended up creating combination operators. “&&” represents the word and. Let’s say you wanted to use 1 and 2. To write that in code, you would write it as 1 && 2. “||” represents the word or. Just like the “&&” operator. These allow us to make our conditional statements (if then statements) more precise and therefore have more functionality.
In coding, there are two basic types of loops. For loops are loops that go on for a specific amount of times which we will call iterations. While loops are loops that go on until a condition is not met. Instead of talking, I’ll just show you a few code segments.
If you plan on pursuing coding, you need to memorize the syntax (coding grammar) of these two types of loops so you can write them at will. These functions are the two most useful functions in all of Java. For the first part of this code segment, there is a for loop that loops six iterations. The way a for loop works is that it sets a temporary variable (in this case i) as 0 and increases the variable after each iteration until it reaches an upper bound (in this case 6). A while loop works by running while a condition is met. This means that while loops can be infinite. In this case, the while loop only runs while k < 9 so once the loop has run nine times and increased k by 9, the loop ends.
Arrays are a fancy word to say list. They are a place where someone can store information. Depending on the data type you set the array as the array will contain different types of information. A key thing to note about arrays is that they have a fixed number of elements inside of them and that when calling its data, the first element is denoted as unit 0 instead of unit 1. Here’s a code segment so I can explain with an example.
To write an array, the syntax for the front will always be “data type [] name =” with the back being what contains the actual data. In the first two arrays colors and magicnumbers, the information is manually imputed by adding data inside of some curly brackets. Since the first array colors is denoted as a string (full words / more than one character), only strings are found within whereas the second array is denoted as an int, so only numbers are found within the second array. The third array is where things get interesting. You can pre-initialize an array with no data by writing that you will create an array of x length. The front part of the array is the standard syntax while the back part is “new int[3];” meaning that this new array is going to be an integer array with a length of three. You can then later set the values of the individual elements as seen below the initialization of wow.
2D arrays follow the same rules as normal arrays except they are two-dimensional. Wow isn’t that crazy? A 2D Array is two-dimensional compared to a normal array! To help better visualize what I’m talking about, here’s a graphic.
The way to write a 2D array is as follows:
This particular array is two rows long and two columns wide. Calling a 2D array is essentially the same as calling a normal array except you have to identify the row and the column from which you are pulling data.
The final topic of this article is ArrayList. ArrayLists are just like arrays except for one key difference. You do not have to set a length for an ArrayList meaning that it can be infinite. The way you write an ArrayList is slightly different from an array. Here’s an example.
To write an ArrayList, the syntax is “ArrayList<DataType> nameOfList = new ArrayList<>();” and to add data to the list is the “nameOfList.add();” function. ArrayLists not needing to set an amount of data allows for them to be versatile in programs that need lists that are always expanding. It’s good practice to use ArrayLists over arrays, but most of the time an array is more than enough so using an array would be easier.
If you made it this far, congratulations! You’ve learned about a whole semester’s worth of coding in two articles. In the next article, I plan on discussing hashmaps, user inputs and outputs, how to build functions, and recursion. If there’s anything you feel that I should’ve clarified, feel free to leave a comment. Thanks for reading and have a great day!