Arrays

__**Array-**__ In computer science it is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type and the array occupies a contiguous area of storage. Most programming languages have a built-in //array// data type. basically, it's a list. It is one variable that stores many things. Can't change the size of an array!

An array is an object that is used to store a list of values. It is made out of a contiguous block of memory that is divided into a number of "slots." Every slot stores a value, and all the values are of the same type. There are two types of arrays: indirect and direct. The difference between these two is that for direct, you have to create and initialize on the other hand indirect you have to create an array but not initialize it. The one thing that you can not do is change the size of the array.

There are 2 types of Arrays: ex: int x = 5; FilledRect = new FilledRect( #, #, #, #, canvas) FilledRect [ ] = {new FilledRect ( #, #, #, #, canvas), new FilledRect ( #, #, #, #, canvas)......};
 * __Direct-__** Create and initialize Arrays

ex: int x; x = 5; FilledRect ; = new FilledRect( #, #, #, #, canvas);
 * __Indirect-__** Creating an array but not initializing it


 * When describing an array, it looks like this:**
 * a || b || c || d || e ||
 * 0th || 1st || 2nd || 3rd || 4th ||
 * the numbering underneath the sample array is the numbering that occurs to each slot for each element. It always starts at zero and works its way up.

When trying to call a method, print, etc. an array, you can only do it one element at a time. You need to indicate which term in the array you are going to use by stating the name ( x ) and then indicating which position in the array it is. ex.** x [0] = a; x [1] = b; x [2] = c; x [4].dance; x [2].move( 1,1 );
 * for calling a method:**

//To Create An Array:// ex.** int [ ] x; is the same as int x [ ]; FilledRect [ ] rectangles; Wombat [ ] y;
 * We use brackets ( [ ] ) either before or after the name of the list.

1. Initialize the array (ex. int [ ] x = ) 2. Create a new array with the number of elements you want in it ( new int [10] ) ex.** int [ ] x = new int [10];
 * In order to create an empty array with a predetermined number of slots or items:

1. Initialize the array (ex. int [ ] x = ) 2. Use braces ( { } ) to enclose the set of values or elements you wish to be inside the array (ex. {1, 2, 3, 4} *just like we've used in math classes) ex.** int [ ] x = {1, 2, 3, 4, 5};
 * In order to create an array with values set in it:

ex.** Mii [ ] myMiis = { new Mii, new Mii, new Mii };
 * In order to create an array with elements that are not integers nor predetermined, we can use the method of initializing the array and then in the braces, call new elements.

You can put **a[11]** or **a[-1]**
 * In order to create a line of code that will cause an array index out of bounds exception.**
 * ex. int[ ]a = new int [10]**

ex.** int a = {0, 1, 2, 3, 4}
 * In order to create a line of code that will create a 5-element array, with each element equal to its index. For example, index 2 equals 2.

ex.** for (int i = 0; i < helloKitty.length; i++)
 * In order to create a loop that will reach every element in an array called helloKitty.

//**Declaring arrays:**// //someType[] a = new someType[ size]; someType[] b = {value 0, value 1, ..., value n-1}//
 * There are two ways to declare and create a one-dimensional array;**

//Loops Using Arrays:// ex.** 0 1 2 3 4 5 6 7
 * When using loops with arrays, the for loop is usually best. You create the loop the same as you would for anything else, but inside the loop, use the name of the array with [ i ].
 * 5 || 8 || 10 || 13 || 16 || 19 || 22 || 25 ||

for ( int i = 1; i <= 6 ; i++) { x [ i ] = x [ i - 1 ] + 1; }

&& if you trace through the loop, you will find that you change x [ 1 ] to 6, x [ 3 ] to 11, and x [ 5 ] to 17.

A shortcut to doing something to every slot in your array is to use a **for loop**. code format="java5" for (int i=1; i < myList.length; i++) {     myList[i]++; //this adds one to each slot in your array } code

__**Practice Problems**__ Create codes using arrays under the following conditions:

returns true if either the first or the last slot is 6. code format="java5" public boolean firstLastIs6(int []nums) {

} code code format="java5" public boolean firstLastIs6(int []nums) {  if(nums[0]==6 || nums[nums.length-1]==6) return true; else return false; } code

returns true if the first and the last slots are equal.

code format="java5" public boolean sameFirstLast(int []nums) {

} code code format="java5" public boolean sameFirstLast(int []nums) {  if(nums[0]>1 && nums[0]==nums[nums.length-1]) return true; else return false; } code

creates and returns an array with the first 3 digits of pi.

code format="java5" public int []makePi {  int[]pi; pi=new int[3]; pi[0]=3; pi[1]=1; pi[2]=4; return pi; } code code format="java5" public int []makePi {  int[]pi = {3, 1, 4}; return pi; } code code format="java5" public int []makePi {  return {3, 1, 4} //this is the cheater's way out } code