What is an Array?

  • Element -> 1 value in an array
  • Index -> the position of the value in the array
  • Declaring an array int[] array = new int[10];
  • Bound errors -> accessing an element that does not exist
  • Uninitialized and Unfilled arrays, assigning an array variable but not the whole array
  • Traversing array, can use while but mainly for loop
  • enhanced for loop .. for (int element:values)

Important Collegeboard

  • array.length(), array[i]
// Write array methods for the Array Methods class below out of the options given above.
public class ArrayMethods {
    private static int[] values = {1,2,3,4,5};

    public static void swap(){
        System.out.println("0th Index = " + values[0]);
        System.out.println("4th Index = " + values[4]);
        int a = values[0];
        int b = values[4];
        values[0] = b;
        values[4] = a;
        System.out.println("0th Index = " + values[0]);
        System.out.println("4th Index = " + values[4]);
    }


    public static void replaceZero(){
        for(int i = 0; i < values.length; i++){
            if(values[i] % 2 == 0){
                values[i] = 0;
            }
        }
        for(int i : values){
            System.out.print(i);
        }
    }
}

ArrayMethods.swap();
ArrayMethods.replaceZero();
0th Index = 1
4th Index = 5
0th Index = 5
4th Index = 1
50301