Thursday, April 16, 2015

blog post about arrays

An array is a "list" of data values ranging from: Strings, Ints, Chars, Doubles, Shorts, and even Objects.


Each item in an array is called an element, and each element is accessed by its numerical index.

The number corresponding to each position is called an index or a subscript.
Array indexes always begin at zero. That means the value stored at index 5 is actually the sixth value in the array.

To find the length you type: y.length; where y is the name.

To access the elements you type: y[z]; where y is the name and z is the index.

To iterate through an array type: for (int i = 0; i < y.length; i++) {} where y is the array.

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.
The declaration of an array first states its data type, using one of the data type keywords, followed by " [] "to express that it will be an array variable

No comments:

Post a Comment