Transcript Arrays

Java Arrays ………….
* arrays are objects in Java
* an array variable is a reference
* an array must be instantiated (created)
An array can store primitive values………….
int[] myarr;
// array reference
myarr = new int[5];
myarr
myarr[0]
myarr[4]
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;
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
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();
}
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
groceries
coffee
tea
bagels
String
objects……
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);
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;
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 ??
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
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 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");
}
If I had an array of Dog objects, would I be able to sort
and search ????????????
need to know about interfaces………………..