Arrays & More In-Depth with Strings

 Arrays & More In-Depth with Strings

Shane Walsh - 10/12/22 - G00406694@atu.ie

What are Java Arrays?

Arrays are a very common way of data structuring. They contain all values of the same type, the items of data put in the array are referred to as elements. First element in an array always starts with index zero, and goes up from there. They inherit the object class and can implement certain interfaces. What you'll see me focus on the most is using them to store primitive values.




As you can see in the image above, arrays must be declared, then constructed, and finally initialized. here are some examples of various code using arrays. Try these out yourself and see what they output!





2D-Arrays:

2D-Arrays or their less common name "Multidimensional arrays" are arrays with a second layer to them essentially, making them 2D. Think of them as like an array, of which every element has another array. If we output this, it gives us a grid like environment. 

Image here

To declare a 2D-Array, we specify each index using 2 square brackets rather then one like previously.

1 Dimensional Array declaration:



2 Dimensional Array declaration:




The same applies to referencing within a 2D-Array. You use two brackets to choose which array you wish to enter, then which specific element within that you wish to call. For Example:

Image here


Strings - More In-Depth:

Now that you understand Arrays, I can explore some of the different methods you can use with String variables. A String variable is actually a created array of characters that is formatted to be used like a variable. But Additionally it can use many different methods that arrays can use.

The String Length method allows us to get the length of a string, it returns the number of characters written within the String.

The String indexOf method gets the index position of the first appearance of a specified thing, such as a character or number.




The String charAt method returns the character from a specified index position. Opposite of the indexOf method. For example if I want the character at index position 0 from the string "Hello", it'll return 'H'.




The String toLowerCase and toUpperCase do exactly what they say on the tin. They change all character of the specified string to lower case letters or upper case ones.




References:

https://www.oracle.com


https://www.w3schools.com/java/default.asp







Comments

Popular Posts