hip hip array!

Download Hip Hip Array!

Post on 12-Jan-2016

23 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Hip Hip Array!. AP Computer Science. What is an array?. Remember Strings? Strings are an array of characters An array is a collection of variables all of the same type. Arrays allow you to store several variables of the same type in one place. The index starts at 0 - PowerPoint PPT Presentation

TRANSCRIPT

  • Hip Hip Array!

    AP Computer Science

  • Remember Strings? Strings are an array of charactersAn array is a collection of variables all of the same type. Arrays allow you to store several variables of the same type in one place.

    37521983260123450123indexElements

    [0] = 3[1] = 7[2] = 9[3] = 2[4] = 1[5] = 5The index starts at 0

    The first item is at index 0

    The last item is at index (length 1)

    Length = 6Index = 6-1=5

    6 elements Index from 0 to 5Array symbol [ ]

  • what type of data can arrays hold?

    String [ ] name = new String [ 3]; or String [ ] name = {Billy, Sally, Bob};

    boolean [ ] done = new boolean [3]; or boolean [ ] done = {true, false, true};

    int [ ] num = new int [5];int [ ] num = {5, 2, 4, 6, 8};

    double [ ] nums = new double [ 5 ]; or double [ ] nums = {2.5, 3.5, 4.5, 5.5, 6.5};primitives or objects

  • What are limitations of arrays? Fixed in sizeeach cell must hold the same type

  • How to declare an arrayString [] name; // just declare String [ ] name = new String [ 3]; // initialize to 3 elements

    name[0] = Billy; // use brute force to assign data name[1] = Sally;name[2] = Bob; Assign the elements when you create the array String [ ] name = {Billy, Sally, Bob);

  • Arrays are never empty? Array names are a reference to memory address When you use the word new a memory address is created.

    double [] salesFigure; no memory address null salesFigure = new double [20]; reserves 20 memory locations for 20 doubles Each element in the array is assigned 0 by default.

    int arrays are filled with ________________

    double arrays are filled with ____________

    boolean arrays are filled with ___________

    object arrays are filled with _____________(Strings are filled with this) 00.0falsenull

  • print array You can print individual elements at specific index locations. array[0]

    if you print the name of the array you get the memory location.

    System.out.println(array); memorylocation

    you must loop over the elements and print each one as you go.

    int [] nums = new int[10];for(int i = 0; i < nums.length; i = i+2)nums[i] = i; System.out.println(nums[i]);

    This will fill each index with a value i+2 and print the array Array will print: 0, 2, 4, 6, 8

    i print < 10i = 0 0 yesi = 0+2 2 yesi = 2 + 2 4 yesi = 4 + 2 6 yesi = 6 + 2 8 yesi = 8 + 2 stop no

  • Reassigning elements in an arrayint[] array = {7,8,10,11,4,3};array[array[0]/2]=15; array[array[4]+1]=9;array[array.length/2-1]=5;array[1]=array[0]+4;

    index 3 = 15 index 5 = 9index 2 = 5 index 1 to be index 4 New Array: 7 4 5 15 4 9 0 1 2 3 4 5

  • Finding how many of a value public boolean findValue(int [] nums, int value){int count = 0;

    for(int i = 0; i < nums.length; i++) { if(nums[i] == 4) count++}return count; }

  • Given an int array length 2, return true if it does not contain a 2 or 3. no23({4, 5}) true no23({4, 2}) false no23({3, 5}) falsepublic boolean no23(int[] nums) { int match = 0; for(int i = 0; i < nums.length; i++){ if(nums[i] == 2 || nums[i] == 3) match++; } if(match > 0) return false; else return true; } }

  • double 23 Given an int array, return true if the array contains 2 twice, or 3 twice. The array will be length 0, 1, or 2. double23({2, 2}) true double23({3, 3}) true double23({2, 3}) falseproblem solve: We want it to count the number of 2s or 3s in the array.

    public boolean double23(int[] nums) { int match2 = 0; int match3 = 0; for(int i = 0; i < nums.length; i++){ if(nums[i] == 2){ match2++;} if(nums[i] == 3){ match3++; } } if(match2 ==2 || match3 ==2) return true; else return false; }

  • Java Subset class java.lang.Mathstatic int abs(int x) static double abs(double x) static double pow(double base, double exponent) static double sqrt(double x) static double random() // returns a double in the range [0.0, 1.0)

  • Produce random using Math.randomMath class double Math.random()Returns a number x in the range, 0.0
  • 2D ArraysTwo-Dimensional Arrays A two-dimensional array is a table/matrix -- with rows and columns -- where each row is an array, and there are several rows in a table. e.g. a two-dimensional array of 3 rows and 4 columns

  • Create 2D ArrayTo create a two-dimensional array, you specify the number ofrows (first) and columns (second). int[][] iMat = new int[3][4]; // integer matrix of 3 rows, 4 columns

    double[][] dMat = new double[5][3]; // double matrix of 5 rows, 3 columns

    char[][] cMat = new char[3][3]; // char matrix -- 3 rows, 3 columns

  • Direct Initialization

    To give initial values for the elements in a two-dimensional array, you can of course set up a loop.Another way is to provide the initial values directly in declaration.

    int[][] iMat = { {12, 48, 69, 7} , {5, 16, 27, 30}, {51, 3, 72, 9} };

  • Access an element in 2D ArrayEach row is an individual array Column index starts at 0

    To access an element in a two-dimensional array, you specify the row index (first) and column index (second). System.out.println(iMat[0][2]); // prints an element at row 0, column 2 (69 in the example above)iMat[2][2] = 5; // assign 5 to row 2, column 1

  • Length 2D arrayLengths of a Two-dimensional Array. Arrays can contain different number of column elements at each row. 0 1 2 3 +----+----+----+----+0 | 12 | 48 | 69 | 7 | +----+----+----+----+1 | 5 | 16 | 27 | | +----+----+----+----+2 | 51 | 3 | | +----+----+----+----+

    LENGTH OF ROW AND COLUMN The Row length (number of rows in the array) is found by using length. int numRow = mat.length; Array to the side has 3 rows The Column Length is different for each row so you must access the row first then the column length. int columnLength = mat[0].length // equal 4 int columnLength = mat[1].length // equal 3

  • Traversing Over a 2D Array with for loop Then we can utilize those two lengths to traverse over a two-dimensional array -- alsoby using anested loop.Two for loops: First specify the row to loop through and then the column. Use Variables row and col to represent the row and column. int row, col; for (row = 0; row < matrix.length; row++) { for (col = 0; col < matrix[row].length; col++) System.out.print[matrix[row][col] + ); } System.out.println(); } int[][] iMat = { {12, 48, 69, 7} , {5, 16, 27, 30}, {51, 3, 72, 9} };

  • ArrayList implements Java.util.List Implements means that the class that implements another class must also implement (use) its methods. ArrayList implements List so it has all the methods from the List interface

    class java.util.ArrayList implements java.util.List

    class java.util.Listint size() boolean add(E obj) // appends obj to the end of list; returns true void add(int index, E obj) // inserts obj at position index (0

  • Facts about ArrayList ArrayList can grow and shrink in size.add, set and remove methods ArrayList use Generics type ArrayList only hold objects Wrapper class Integer and Double ArrayList uses size() method to find the size (length) of the array). Array class used length variable

  • Creating an ArrayList ArrayList contain elements of type E Generics: the collections classes are generic, with type parameters. The type parameter is replaced by an actual object type.

    The is replaced with the type of object. ArrayList list = new ArrayList();

  • Boxing and unboxing Autoboxing is the automatic conversion from a primitive type to its Object type. For example, converting an int to an Integer, a double to a DoubleIf the conversion goes the other way, this is called unboxing.List li = new ArrayList(); for (int i = 1; i < 50; i += 2) li.add(i); int x = li.get(i); i is converted to the Integer object type

    i is converted back to an integer (int)

  • Wrapper Classes class java.lang.IntegerInteger(int value) int intValue() Integer.MIN_VALUE // minimum value represented by an int Integer.MAX_VALUE // maximum value represented by an int

    class java.lang.DoubleDouble(double value) double doubleValue()

    public int min(int [] nums) { int min = Integer.MAX_VALUE; for(int i = 0; i < nums.length; i++) { if(min > nums[i]) min = nums[i]; } return min; }

  • Example Arraylist list = new ArrayList ();list.add(one);list.add(two);list.add(three);list.remove(0);list.set(1, one); System.out.println(list);

    oneonetwoonetwothreetwothreetwoonethreetwoonethree

  • What prints? ArrayList words = new ArrayList(); words.add(Zack); words.add(Bobby); words.add(0, "Billy"); words.add(1, "Sam"); words.add(2, "Jane"); words.add(1, "Johnny"); words.remove(2); words.set(1, "Tammy"); words.add("Sally");What will print? Figure this out and earn extra points on your ArrayList Quiz

  • Printing ArrayList Printing out an Ar

Recommended

View more >