Arrays for beginners

By:Mohammad-Ali Bandzar | May 9 2019

An introduction to Arrays with java

Have you ever been in a situation where you had a large number of strings and had difficuilty managing it all. Well thats what arrays are all about, An array is like a textbook where each page is its own uniquely addressed and accessible string, but are all stored under one main name, where the textbook name would be the array name. In java arrays are only used to store a single data type such as double or string, however there are two main types of arrays in java, Arrays and array lists.

Arrays: these are used to store data of a single type often a primitive type such as double or int. These kinds of arrays have a fixed size that must be declared when they are created. An array of this type can also contain arrays of any size of the same type, each array contained within an array adds a dimension to the array as explained below:

1D Arrays: a one dimensional array allows you to store a single set of values, kind of like a list youd make before you went grocery shopping. all the items are placed back to back in a sequencial order. They effectively replace the need for you to create a series of variables the same type and allow you to group your data together into an umbrella name, the array name.

2D Arrays: this is an array that contains an array for ever element within it. it allows you to store values in a similar fashion to what a table would allow you to do. a good example use case for a 2d array would be if you wanted to make student names searchable by seat position in the class, you could create a 2d array of strings that contains the names of students in their assigned seats and then the array would be searchable by seat placement. making locating which students are seated closest to them easy.

Array Lists: This type of array is best suited for situations where you want to use an array but are unable to determine the required length of the array during initialization. an array list is an array that automatically resizes when it becomes full.

Examples

initializing a 1D array:

        private DataType [] arrayName = new DataType [arraySize]
    

initializing a 2D array:

        private DataType [][] arrayName = new DataType [NumOfRows][NumOfColums]
    

initializing an arraylist:

        ArrayList<ElementType> arrayListName = new ArrayList<ElementType>();
    

determining an array length:

        arrayName.length;
    

determining size of an arraylist

        arrayListName.size();
    

Adding an element to an array list

        arrayListName.add(Element);
    

Getting a value from an array list

        arrayListName.get(Location);
    

determining if an array list contains a value

        arrayListName.contains(Element);
    

finding the index of a value in an array list

        arrayListName.indexOf(Element);
    
Foreach loops

The Foreach loop is the same thing as a foorloop that would go through every element of the array, but instead of passing the index value to the contained code, this passes the value contained within the array. Simplifying the code and making it look more simple.

        for (ElementType tempElementName: arrayListName){

        }
    

THANKS FOR READING
credits:https://en.wikipedia.org/wiki/Foreach_loop

Array descrption