top of page
Writer's picturePraveen Mogan

Arrays

From here on out, learning Java is a downhill battle. Everything after classes is a lot easier to learn, except inheritance, but we will take care of that later. With that said, pat your self on the back, get comfy, and let's get started on learning about arrays.


As always, let's get the vocab out of the way (it's not that bad this time):


Array - are used to store multiple values of a single type into one variable

Element - each value stored by an array

Index - the location where variables are stored


So, Arrays are data structures that can store values based on their indexes. You can store values within each index, which can be altered or changed. However, you CAN'T add or remove indexes after defining an array's size.


Note: when you declare an array without values, all values will be set to 0, false, or null based on the data type of the array


Other important notes:

- arrays can store primitive or objects

- can't be resized after creation

- store and access values based on indices, locations where values are stored

- each value stored by an array is called an element

- the first index in an array is at index 0

- the last index is (number of elements)-1 (Ex. last index in 8 element array has starting index at 0 while the ending index is 7)


Syntax:

defining an array without values - type[] name = new type[index];

defining an array with values - type[] name = {values,value,...};


Example:

char[] alphabet = new char[3]; //no values assigned

char[] alphabet = {'a','b','c'}; //values assigned


accessing a value:

arrayName[indexNumber]; (Ex. alphabet[3];)


Changing a value:

arrayName[indexNumber] = newValue; (Ex. alphabet[3] = 'b';)


Accessing length:

arrayName.length (Ex. alphabet.length;)


For loop:

for(int x = 0;x<name.length;x++)

System.out.println(name[x]);


For each loop:

for(typeOfData variableName: arrayName)

System.out.println(variableName);


for(char c: alphabet)

System.out.println(c);


Turning a String to a char array:

char[] arrayName = stringName.toCharArray();

char[] charArray = "blob".toCharArray();


Now that we have covered everything on 1D arrays, let's go onto 2D arrays:


Creating a 2D array:

type[][] name = new type[numRow][numCols];

char[][] tictactoe = new char[3][3];


Creating a 2D array from values:

type[][] name = {{value, value,...},

{value, value,...},

{value, value,...}};

int[][] numGrid = {{1,2,3},

{4,5,6},

{7,8,9}};


Accessing rows from a 2D Array:

name.length


Accessing cols from a 2D Array:

name[0].length


Note: Rows go across; columns go down


Accessing an element in a 2D Array:

name[row][col];


Changing a value in a 2D array:

name[row][col] = newValue;


Printing all values with a for loop:

for(int r = 0;r<name.length;r++)

{

for(int c = 0;c<name[0].length;c++)

{

System.out.println(name[r][c]);

}

}


Printing all values with a for-each loop:

for(int[] row: numGrid)

{

for(int item:row)

{

System.out.println(item);

}

}


With that said, arrays are really powerful in data maintenance and storage. Whenever you need to organize a set number of elements, arrays will make your life easier 99.9% of the time.


I hope it helps and happy coding/cramming! Until next time.


42 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page