java arrays …………. java arrays …………. * arrays are objects in java * arrays are...
TRANSCRIPT
![Page 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](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/1.jpg)
Java Arrays ………….Java Arrays ………….
* arrays are * arrays are objectsobjects in Java in Java
* an array variable is a reference* an array variable is a reference
* an array must be instantiated * an array must be instantiated (created)(created)
![Page 2: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/2.jpg)
An array can store primitive values………….
int[] myarr; // array reference
myarr = new int[5];
myarr
myarr[0] myarr[4]
![Page 3: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/3.jpg)
int [] myarr = new int[5];
//initialize arrayString 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 arrayString 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 arrayfor (int index = 0; index < myarr.length; index ++) myarr[index] = 100;
![Page 4: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/4.jpg)
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
![Page 5: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/5.jpg)
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();}
![Page 6: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/6.jpg)
bagelsbagelscoffeecoffee
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 implementation
teatea
groceries
String objects……
![Page 7: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/7.jpg)
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);
![Page 8: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/8.jpg)
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 arrayint index = 0;int numItems = 0;
while (item != null) { groceries[index] = item; index++; item = in.readLine();}int numItems = index;
![Page 9: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/9.jpg)
Each item of the array groceries is a String object, and can be usedto call String methods……
for (i=0; i < numItems; i++) groceries[i] = groceries[i].toUpperCase();
What would happen if I replaced numItems with groceries.length ??
![Page 10: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/10.jpg)
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
![Page 11: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/11.jpg)
Code which stores, sorts and then searches thru data is easy…….for (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 arraywhile ((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"); }
![Page 12: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/12.jpg)
If I had an array of Dog objects, would I be able to sortand search ????????????
need to know about interfaces………………..
![Page 13: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/13.jpg)
![Page 14: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/14.jpg)
![Page 15: Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is](https://reader036.vdocuments.mx/reader036/viewer/2022082407/56649ed85503460f94be6c19/html5/thumbnails/15.jpg)