1 / 7

Arrays

Learn about arrays, a collection of elements with the same data type, accessible through indexing. Discover different ways to declare, initialize, and work with arrays, as well as multidimensional arrays and polymorphism in Java.

carldavis
Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays • array is a collection of elements • they have the same data type • indexed, i.e. accessible through an index • elements can be • objects of the same class or interface • values at a primitive type • index is an integer • numbering starts with 0 • the first element has index 0 • the last element has index N-1, where N is the size of the array • i.e. range of the indices is 0, 1, 2, ..., N-2, N-1 • the size of an array doesn't change

  2. Array Syntax • there are two ways to declare it: • with [] before the name, e.g., String [] names; • with [] after the name, e.g., double scores []; • use index within [] to access an element • e.g., names [0] = "Jan"; • e.g., System.out.println ("Jan got " + scores [4]); • there are two ways to initialize an array: • by stating all of its elements within {}, e.g., • String [] names = {"Jan", "Bob", "Bob"}; • by declaring its size, e.g., • double [] scores = new double [10];

  3. Arrays Initialization • initialization with new fills the array with the default values • e.g., new double [2] is the same as {0.0, 0.0}; • e.g., new String [3] is the same as {null, null, null}; • problem with array initialization • only in declaration you can use the {} form • e.g., this is illegal: • double grades [];grades = {3.33, 3.56, 1.29}; • but this is ok: • grades = new double [] {3.33, 3.56, 1.29};

  4. Arrays (cont.) • an array is an object • e.g., String names [] = {"Jan", "Bob", "Bob"}; • then names references the object {"Jan", "Bob", "Bob"} • an entire array can be accessed, not only its elements • it can be assigned • e.g., if String names2 [] = names; • then names and names2 reference the same object • an array can be passed as a parameter to a method • e.g., void write (String names[]) {...} • every array has a length field • watch out, the element with index length doesn't exist! • the last index is length-1 • e.g., names[names.length-1] is the last element

  5. Arrays in for-Loops • to go through an array: • for (int i = 0; i < names.length; i++) {System.out.println (names[i]);} • simpler way: "for-each" form: • for (String name: names) {System.out.println (name);} • this is equivalent • to go through an array backwards: • for (int i = names.length - 1; i >= 0; i--) {System.out.println (names[i]);}

  6. Multidimensional Arrays • an array can hold other arrays • e.g. with • String sue [] = {"Sue", "Kim"}; String ev [] = {"Ev", "Man"}; • we can declare: • String persons[][] = {sue, ev}; • or simpler: • String persons[][] = {{"Sue", "Kim"}, {"Ev", "Man"}}; • think of rows and columns • "Sue", "Kim""Ev", "Man" • access as array • persons[1][0] // is Ev" • the length of subarrays can be different • e.g. triangular matrix • int matrix [][] = {{"1", "2", "3"}, {"4", "5"} {"6"}};

  7. Arrays and Polymorphism • an array declared with a superclass type can hold objects of subclass type • class Square exends Shape {…}class Oval exends Shape {…}Shape shapes []{new Square(), new Oval()}; • then we can use it in a for-loop • for (Shape shape: shapes) {shape.draw();} • this will draw all the shapes • if subclass overridesdraw()thenitsdraw()will beused • similarly, an array declared with an interface type can hold objects of any class that implements it • class Square implementsColorable {…}class Oval implementsColorable {…}Colorable colorables []{new Square(), new Oval()};for (Colorable colorable: colorables) {colorable.setColor(Color.pink);}

More Related