Thursday, March 26, 2015

Array Explanation

Here's some random things that I know about arrays.

An array is a "list" or groups of values, and can be in any type of value. [0] is the first index.

There are int[], String[], boolean[] arrays and many more.

You can declare a array in two ways:

int[] testArray;
or
int testArray[] = new int[5];

The second way will create an array with 5 empty values. You must declare an amount of values. the array cannot change size after it has been created.

testArray.length; // the length of the array

You can change a value using testArrat[n] where n is the index of the value you wish to change;

testArray[2] will return 6, for example.
testArray[2] = 4; it will now return 4.

You can't just print an array. However, you can iterate through an array like this:

for (int i: testArray) {
   System.out.println(i); // this will print every value inside of testArray on a new line.
}

To compare values, you would still abide by the normal procedures. If you have a String[] array, you'd use .equals, and == for mostly anything else.

These are some more topics that should have been learned:

  • To use arrays to store data in and retrieve data from lists and tables of values. 
To store, you'd put testArray[0] = 1; and to retrieve you'd put int variable = testArray[0].
  • Pass arrays to methods. 
To do this, you'd create a method that takes in an array. You can return arrays too, but you can't print them.

1 comment: