Tuesday, March 31, 2015

Arrays!

What is an Array?

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

Why bother using an array if I can just use normal variables?

By using arrays to can easily make lists of variables using only one command. for example I could use: String x[] = {"a","b","c"}: to create an array and reference it using x[1]. By doing so you can refer to variables quickly and without using too much space in a large program.

So how exactly do you reference variables in an array?

In Java all values, or data stored in an array is assigned a number value starting with 0. If i were to print x[0] from above the output would be "a". If i were to print x[1] the output would be "b".

Okay... so whats the catch?

Lets say you want to have a user input Int data to be organized into an array. To do so you would have to declare an empty array: intArray = new int[10] (the 10 specifies how long the array is, or how many different Int's you can store in it). This basically makes an array with 10 slots for data, and NONE MORE. Java can't keep adding slots to an array after your specified amount is entered. In Java you also cant store different types of data in the same array. If you declare the array to be an int array, the only values it can store are ints.