Arrays & Vectors
Download
Report
Transcript Arrays & Vectors
Single Instances - Reminder!
int anInt = 6;
anInt
6
6
anInt
int
double aFloat = 12.34;
aFloat
12.34
double
aString
String aString = “Hello Mum”;
aString
LimitedCounter aCounter =
new LimitedCounter( 10, 20);
“Hello Mum”
aCounter
counted initial
10
10
max
min
12
10
Multiple Instances - Problem!
String
String
String
String
student1
student2
student3
student4
=
=
=
=
“Abe Abbey”;
“Bev Buddy”;
“Chas Chin”;
“Don Duppy”;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
String student26 = “Zac Zacky”;
student1
“Abe Abbey”
student2
“Bev Buddy”
student3
“Chas Chin”
student4
“Don Duppy”
How about all the students on this unit?
Or all the students on the computing degree?
Or all the students in the School of Computing?
Or all the students in the University?
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
~~~~~~~~~
student26
“Zac Zacky”
Multiple Instances - Solution?
String allStudents[] = {
“Abe Abbey”, “Bev Buddy”, “Chas Chin”,
“Don Duppy”, ~~~~~~~~~~ , “Zac Zuppy”
allStudents
0
“Abe Abbey”
1
“Bev Buddy”
2
“Chas Chin”
3
“Don Duppy”
25
~~~
~~~
~~~
~~~
“Zac Zacky”
allStudents is an iterative data structure which contains 26 String
instances. Each instance is anonymous and identified by its location
(0 to 25). Data structures declared with [ ] are known as arrays.
List
0
1
2
3
4
5
6
7
3
7
6
8
4
1
5
2
int list[] = new int[8];
list[5] = 1;
list[1] = 7;
list[4] = list[5] + 3;
list[0] = 3;
list[7] = 2;
list[2] = list[0]*list[7];
list[6] = 5;
list[3] = list[4] * 2;
int pos = 7;
for(int x = 0; x < 8; x++){
System.out.print(list[pos]);
pos--;
}
pos = 7 x = 0
list[7] --> 2
pos = 6 x = 1
list[6] --> 5
pos = 5 x = 2
list[5] --> 1
list[4] --> 4
pos = 4 x = 3
pos = 3 x = 4
list[3] --> 8
pos = 2 x = 5
list[2] --> 6
pos = 1 x = 6
list[1] --> 7
pos = 0 x = 7
list[0] --> 3
String list[] = new String[8];
list[5] = new String("Abad");
list[1] = "Gina";
System.out.println( list[5]);
System.out.println( list[0]);
list[1] = list[5];
System.out.println( list[5]);`
Abad
Gina
0
1
error!!!
2
3
4
5
6
7
Print a List of Students
for( int thisStudent =0;
thisStudent < allStudents.length;
thisStudent++) {
System.out.println( (thisStudent+1) +
allStudents[ thisStudent]);
} // End for.
1
2
3
4
Abe Abbey
Bev Buddy
Chas Chin
Don Duppy
26 Zac Zuppy”
Arrays - another example
int waypointMarks[];
Declares - but does not create an array.
waypointMarks[] = new int[ 20];
Creates an array with 20 (0 to 19) integer instances.
waypointMarks[ 12] = 68;
Sets the value of the element at location 12 to 68.
totalMarks = 0;
for ( int index =0; index < 20; index++ ) {
totalMarks += waypointMarks[ index];
} // End for.
averageMark = ((double) totalMarks) / 12;
Calculates the average of all the values in the array.
Arrays
The size of an array is fixed at the time it is created
and cannot be changed afterwards.
All the elements stored in the array must be of the same
type or class. (It is a homogenous data structure.)
An array has a public attribute called length which indicates
the number of elements that it contains. (Although, strictly,
an array is not an Object).
The elements contained in an array can also be arrays! (And
the elements in those arrays can be arrays, and so on …). These
are known as multi-dimensional arrays.
Any attempt to access an element outside the length of an
array will throw an ArrayIndexOutOfBoundsException.
Book references: Chapter 6 & Chapter 13.
Sorted!!
int
demo[] = new int[ 8];
int
temp
= 0;
boolean sorted = true;
System.out.println( "\n\nUnsorted ...\n");
for ( int index =0; index < demo.length; index++) {
demo[ index] = (int) (Math.random() * 100.0);
System.out.println( demo[ index]);
} // End for.
sorted = false;
while ( !sorted) {
sorted = true;
for ( int index =0; index < demo.length -1; index++) {
if ( demo[ index] > demo[ index+1]) {
sorted = false;
temp
= demo[ index];
demo[ index]
= demo[ index+1];
demo[ index+1] = temp;
} // End if.
} // End for.
} // End while.
Sorted - design
c1
*
sort iteration
set sorted
true
check
the list
check * c2
a pair
c1: While list is not known
to be sorted.
c2: For every pair.
c3: If pair not in order
swap o c3
a pair
set sorted
false
swap
pair around
Limitations of Arrays
Arrays are supplied by (almost) every programming environment
and so are understood by (almost) every developer.
But they are probably not the best first choice for an
iterative structure in Java.
Java supplies a number of different collection classes in the
java.util package.
A java.util.Vector instance can be used (almost) everywhere an
array can be used and is probably a better choice.
A java.util.Vector instance is an Object!
Arrays cf Vectors
Arrays
Vectors
Fixed size
Grow and shrink as required
Language feature
Object
Homogenous
Hetrogenous
Store Objects or primitives
Store Objects only
Unique protocol
Common protocol
(As vectors cannot store primitves at all, this makes an array
a more favoured choice for storing simple numerical information!)
java.util.Vector
Vector() // Default constructor - makes an empty vector
public boolean add( Object toAdd) // Add to end of existing contents.
public void
add( int position, Object toAdd)
// Add at position specified.
public void clear() // remove all elements.
public Object firstElement()
public Object lastElement()
public Object elementAt( int position) // retrieve element.
public Object remove( int position) // remove element.
public void setElementAt( Object newOne, int position) // replace element.
public boolean isEmpty() // inquiry method.
public int size() // inquiry method.
As would be expected the position values start at zero.
Sorted again!!
Vector demo
= new Vector();
Integer temp
= null;
boolean sorted = true;
System.out.println( "\n\nUnsorted ...\n");
for ( int index =0; index < 20; index++) {
demo.add( new Integer( (int) (Math.random() * 100.0)));
System.out.println( ((Integer) demo.elementAt(
index)).intValue());
} // End for.
sorted = false;
while ( !sorted) {
sorted = true;
for ( int index =0; index < demo.size() -1; index++) {
if ( ((Integer) demo.elementAt( index)).intValue() >
((Integer) demo.elementAt( index+1)).intValue()) {
sorted = false;
temp
= (Integer) demo.elementAt( index);
demo.setElementAt( demo.elementAt( index+1), index);
demo.setElementAt( temp, index+1);
} // End if.
} // End for.
} // End while.