Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.

Similar presentations


Presentation on theme: "Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is."— Presentation transcript:

1 Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is a reference * an array must be instantiated (created) * an array must be instantiated (created)

2 An array can store primitive values…………. int[] myarr; // array reference myarr = new int[5]; myarr myarr[0] myarr[4]

3 int [] myarr = new int[5]; //initialize array String fiveletterword = JOptionPane.showInputDialog(“Enter 5 letter word”); for (int strpos=0; strpos < 5; strpos++) myarr[strpos] = fiveletterword.charAt(strpos); //what’s going on here //print out the ASCII values in the array String temp = “”; for (int index=0; index < 5; index ++) temp = temp + “ “ + myarr[index]; System.out.println(temp); //what is printed, numbers or chars? // using length field of an array for (int index = 0; index < myarr.length; index ++) myarr[index] = 100;

4 Can also create an array object by providing initial values: double[] values = { 5.5, 6.1, 0.0, 6.1, 4.3, 7.1}; values is an array of length 6, with legal indices 0 to 5

5 An array can be declared to store any type … class types too. import java.io; public class demo{ public static void main (String [] xx) throws IOException { String[] groceries = new String [100]; //each element of groceries is a reference to an object FileReader instream = new FileReader(“list.txt”); BufferedReader in = new BufferedReader(instream); String item = in.readLine(); int index = 0; while (item != null) { groceries[index] = item; index++; item = in.readLine(); }

6 bagels coffee FileReader instream = new FileReader(“list.txt”); BufferedReader in = new BufferedReader(instream); String item; int index = 0; while ((item = in.readLine() )!= null) groceries[index++] = item; //alternative loop implementationtea groceries String objects……

7 Assuming the array groceries.. What would the following print to the screen……. for (int i= 0; i < groceries.length; i++) temp = temp + “ “ + groceries[i]; System.out.println(temp);

8 for (int i= 0; i < numItems; i++) temp = temp + “ “ + groceries[i]; System.out.println(temp); coffee tea bagels null null null null null null null (90 more times) String item = in.readLine(); //code which stored values in array int index = 0; int numItems = 0; while (item != null) { groceries[index] = item; index++; item = in.readLine(); } int numItems = index;

9 Each item of the array groceries is a String object, and can be used to call String methods…… for (i=0; i < numItems; i++) groceries[i] = groceries[i].toUpperCase(); What would happen if I replaced numItems with groceries.length ??

10 The java.util package provides a class named Arrays. This class is full of STATIC METHODS which work with array parameters………… http://java.sun.com/j2se/1.4.2/docs/api/index.html

11 Code which stores, sorts and then searches thru data is easy……. f or (index = 0; index < numItems; index++) temp = temp + " " + groceries[index]; System.out.println("groceries are" + temp); String [] groceries2 = new String[numItems]; System.arraycopy(groceries,0, groceries2, 0, numItems); Arrays.sort(groceries2); //must sort and search full array while ((item=JOptionPane.showInputDialog("Enter item to look for ")) != null){ index = Arrays.binarySearch(groceries2,item); if (index >=0 ) JOptionPane.showMessageDialog(null, item + " is there"); else JOptionPane.showMessageDialog(null, item + " is not there"); }

12 If I had an array of Dog objects, would I be able to sort and search ???????????? need to know about interfaces………………..

13

14

15


Download ppt "Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is."

Similar presentations


Ads by Google