Hello dear and welcome back to my blog where i make you make sense outta your Java programming journey. Trust me, starting out programming with Java(“especially when its your first time to programming”) can be stressful and abstract(“nothing seems to make real sense to you”). That’s why some people like me who have passed some hurdles in learning Java(“am still learning”) have decided to share knowledge with you, we’ve decided to take out the pain of trying to understand Java concepts by presenting it to you in a lay mans way.
Quickly, lets dive into the topic of today, which happens to be Arrays in Java. What is an array in Java, why, when and how does it come in handy. These are the nitty gritties of an array in java that will be broken into bits for your quick and easy understanding.
Now, a prerequisite for understanding this concept in java is a sound knowledge of the primitive datatypes in java…. if you dont know or are still shaky on the concept?? no worries, i got you undercover CLICK HERE for your access to that knowledge.
If you are all set with the primitive datatype knowledge, then lets talk arrays.
An array in java is simply a collection of the same primitive datatypes all stored in a particular variable. Hmmm ,so previously, in the primitive datatypes post, we saw that in java we could store single values in a particular variable with the datatype of such value being set to match that of its value. Now the smart people over at Java, thought it wise to also create another way in which creative software developers like you and me could not only store one value of a certain primitive datatype but also a collection of it. Hence the existence of an array in java.
Note: When storing values in an array, be sure to store only values of the same primitive datatype.
So how does an array in java work….. lets take a look the picture below.
Looking at the pic above you can see that declaring and assigning values to an array in java is quite different from the way we do single value variables. Commit to memory the way the code is written above, see how the datatype of the array is declared followed by the square brackets(“that is the symbol for arrays in java, it is also used to represent an index in an array”) next up is the name of the array and then the assignment operator and finally the values to be stored in the array which are surrounded by a set of curly braces.
Why the curly braces??… well thats how you write out a series of related values to be stored in an array in java….anything aside the curly braces, is invalid and a considered a syntax error in java…. Also thats not the only way to store values in an array… heres another way.
Look carefully at the first line in all three images. Apart from the left hand side of the declaration of the arrays, notice how the right hand side has the new java keyword(“the keyword is used to create objects in java”) followed by a number inside a squared bracket. So basically, in java an array always has a fixed size(“this can later be resized”) which is determined by you when you either implicitly do so using the first way of initializing an array or explicitly with second way. Now you can use any of these ways of declaring and assigning an array in Java, it all depends on how you want it.
Note: when using the second way of creating an array, you must set the size of the array else the compiler will complain and tell you to initialize the array with a size
Note: spaces(“index as is called in Java”) in an array in java are counted starting from the number zero(“coz thats how computer scientist thought us to count in programming”) so when using the second array approach, be sure to assign your first value to the index at 0(“as is done in the above pic”) and so, will you continue to assign more values to indexes of your choice.
To further solidify your understanding of arrays in Java, let me show you a graphical image of how values are stored in an array.
To better understand the concept of an array, commit this image above to memory. Looking at the image, you can see that we have five(5) color boxes stored in the array but the indexing stops at four(4). This is because we start counting our values in the array from zero(0) and the last item in the array, will always be at an index which will be the value of subtracting one(1) from the number(“length”) of items stored in your array.
Note: in Java arrays, how you choose to order your values in the array, is up to you as long as you don’t specify an index that is not presently existing in your array. Doing that will cause the Java Virtual Machine to throw an ArrayIndexOutOfBounds Exception
Also arrays in java, have only method that can be called on them. This method is known as the .length() which obviously is used to determine the length of an array(“which is different from the index size of the array”).
Further more, let me draw your attention to how to handle major operations in arrays. Most of the operations you would want to carry out with an array would involve transferring values from one array to another, resizing the array, adding values to the array, retrieving values from the array, modifying a value in the array and many more things which i dont think for now may be possible(“explore your creativity”).
Moving, resizing, adding to and retrieving from an array, all require you to use a loop(“most preferably a for loop..click here to learn about it”). Now if you have a sound knowledge about loops in java, you obviosly know that a for loop is used when you know how many times your loop would run. So putting one and two together, since an array has a fixed size set by you then it suffice to say that a for loop would best fit in handling the above stated operations(“this is because the loop is expected to run as long as the iterator value is less then the length of the array else the loop breaks”).
Below is an example of what using an array in a for loop looks like.
See how i was not only able to set the loop condition to run only if the iterator value is less than the length of the array, i was also able to make sure that the iterator value was set to zero so as to enable the print method continue to print out values located in the current array index based on the value of the iterator. On the right hand side of the above pics, you can see that an enhanced for loop can also be used but has limitations to its use in some array operations see here for reason.
Here is the LINK to see codes on how to perform all of the above stated array operations.
Note: this is unusual, i don’t actually show codes.
Note: An array in java cant store objects from classes you created.
This is all about array that should get you up and running in any array related issues in Java. Bye for now….