Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an.

Download Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an.

Post on 20-Jan-2016

218 views

Category:

Documents

0 download

TRANSCRIPT

  • Primitive ArraysA primitive array stores multiple values of the same primitive data type.The index of the firstposition in an array is 0.

    Introduction to Object-Oriented Programming with Java--Wu

  • Accessing Individual ElementsIndividual elements in an array accessed with the indexed expression.

    Introduction to Object-Oriented Programming with Java--Wu

  • Array Lengths

    Introduction to Object-Oriented Programming with Java--Wu

  • Example Programs Phrrud out to reality ArrayRain.javaPhrrud out to reality ArrayAverages.java

    Introduction to Object-Oriented Programming with Java--Wu

  • Array Bounds Errors Trying to access an array element that does not exist causes a runtime errorNegative indicesIndices beyond the sizeFalop out to reality ArrayBoundsError.java

    Introduction to Object-Oriented Programming with Java--Wu

  • Array InitializationLike other data types, it is possible to declare and initialize an array at the same time.The capacity of the array is set to the number of elements in the list.

    Introduction to Object-Oriented Programming with Java--Wu

  • Example Programs Groeeet out to reality ArrayInit.java

    An array variable can be explicitly made to point to no data, using the null valueSpaaocie out to reality ArrayNULL.java

    Introduction to Object-Oriented Programming with Java--Wu

  • References are Pointers A reference variable points to an objectSo, arrays are objects, but don't worry about that nowBut it does mean you canHave multiple references to an arrayNot copy an array with =Lose an array

    Introduction to Object-Oriented Programming with Java--Wu

  • Having Two References to an ArrayCodeState of Memory

    Introduction to Object-Oriented Programming with Java--Wu

  • Example Program Dooop out to reality ArrayDup.java

    Introduction to Object-Oriented Programming with Java--Wu

  • Cloning an ArrayAn array can be copied using the clone methodIt's necessary to cast the clone to the right array typeBabababoom out to reality ArrayClone.java

    Introduction to Object-Oriented Programming with Java--Wu

  • Losing an ArrayCodeState of Memory

    Introduction to Object-Oriented Programming with Java--Wu

  • Garbage Collection An array that has no references is garbage collected by the java programSpaaocie out to reality ArrayGC.java

    Introduction to Object-Oriented Programming with Java--Wu

  • Two-Dimensional ArraysTwo-dimensional arrays are useful in representing tabular information.

    Introduction to Object-Oriented Programming with Java--Wu

  • Example Programs Ieeei out to reality ArrayMatrix.javaIeeei out to reality ArrayCalendar.javaIeeei out to reality ArrayCube.java

    Introduction to Object-Oriented Programming with Java--Wu

  • Multi-dimensional Arrays NOT Java does not really have multi-dimensional arraysJava has arrays of arraysint[][] data = new int[3][5];is shorthand forint[][] data = new int[3][];data[0] = new int[5];data[1] = new int[5];data[2] = new int[5];

    Introduction to Object-Oriented Programming with Java--Wu

  • Multi-dimensional Arrays in RAM int[][] data = new int[3][5];Zuuu out to reality ArrayRAM.java

    Introduction to Object-Oriented Programming with Java--Wu

  • Irregular Arraysint[][] weirdData = new int[3][];weirdData[0] = new int[5];weirdData[1] = new int[4];weirdData[2] = new int[7];

    Introduction to Object-Oriented Programming with Java--Wu

  • Irregular Arraysint[][] weirdData = new int[3][];weirdData[0] = new int[5];weirdData[1] = new int[4];weirdData[2] = new int[7];weirdData.length == 3weirdData[1].length == 4Jioooul out to reality ArrayIrreg1.java

    Introduction to Object-Oriented Programming with Java--Wu

  • Passing Arrays to Methods - 1CodeState of MemoryminOne = searchMinimum(arrayOne); public int searchMinimum(float[] number)){

    }AA. Local variable number does not exist before the method execution

    Introduction to Object-Oriented Programming with Java--Wu

  • Passing Arrays to Methods - 2CodeState of MemoryminOne = searchMinimum(arrayOne); public int searchMinimum(float[] number)){

    }B. The value of the argument, which is an address, is copied to the parameter.

    Introduction to Object-Oriented Programming with Java--Wu

  • Passing Arrays to Methods - 3CodeState of MemoryminOne = searchMinimum(arrayOne); public int searchMinimum(float[] number)){

    }C. The array is accessed via number inside the method.

    Introduction to Object-Oriented Programming with Java--Wu

  • Passing Arrays to Methods - 4CodeState of MemoryminOne = searchMinimum(arrayOne); public int searchMinimum(float[] number)){

    }D. The parameter is erased. The argument still points to the same object.

    Introduction to Object-Oriented Programming with Java--Wu

  • Example ProgramsFlunot out to reality ArrayParamAvg.javaFlunot out to reality ArrayParam1.java

    Introduction to Object-Oriented Programming with Java--Wu

  • Returning ArraysArray variables in methods exist until the method ends, but the array data lives while referencedAn array variable can be returned from a methodThe receiving array variable then refers to the array data, and the array persistsWrrbbrack out to reality ArrayReturn.javaWrrbbrack out to reality ArrayParam2.java

    Introduction to Object-Oriented Programming with Java--Wu

  • Local arrays Array variables in methods exist until the method endsThe array data referred to by such an array variable is lost and garbage collected when the method endsDessserts out to reality ArrayLocalGC.java

    Introduction to Object-Oriented Programming with Java--Wu

    Chapter 9Intro to OOP w/Java--WuChapter 9Intro to OOP w/Java--WuChapter 9Notice the public constant length returns the capacity, not the actual number of non-blank values in the array.Intro to OOP w/Java--WuChapter 9Intro to OOP w/Java--WuChapter 9Intro to OOP w/Java--WuChapter 9Intro to OOP w/Java--WuChapter 9Intro to OOP w/Java--WuChapter 9Intro to OOP w/Java--WuChapter 9When an array is passed to a method, only its reference is passed. A copy of the array is NOT created in the method.

    public int searchMinimum(float[] number){int indexOfMinimum = 0;

    for (int i = 1; i < number.length; i++) {if (number[i] < number[indexOfMinimum]) { //found a indexOfMinimum = i; //smaller element }}return indexOfMinimum;}Intro to OOP w/Java--WuChapter 9Intro to OOP w/Java--WuChapter 9Intro to OOP w/Java--WuChapter 9Intro to OOP w/Java--Wu

Recommended

View more >