Title: CS3006N Mass Storage Section 11
1CS3006N Mass Storage Section 11
- James King
- 12 August 2008
2Mass storage - Arrays, Strings and Vectors
3Arrays
4Arrays
- Arrays allow large numbers of simple data types
such as int or instances of classes to be easily
accessed. - int arraynew int10
- array04
- array91234
- System.out.println(array0)
indicates that the array will hold 10 items
indicates variable is an array
accessing an element of an array also uses
arrays start at 0
this is the last element of the array
5Arrays holding Objects
- Arrays can also hold instances of a class or any
of its subclass - Monster arraynew Monster10
- array0new Monster()
- array9new Dragon()
- array0.take_damage()
indicates that the array will hold 10 instances
of the Monster class or subclass
Dragon is a subclass of Monster so this is fine
array0 is an instance of Monster so we can call
take_damage on it
6Dangers of Arrays of Objects
- with an array of simple types such as ints every
element in the array exists even if we dont
assign a value to them - int arraynew int2
- System.out.println(array0)
- with an array of Objects elements are null until
we assign an instance to them - Monster arraynew Monster2
- array0.take_damage()
no problem!!
null pointer exception!
7Initialising Object Arrays
- unlike arrays of simple types arrays of any type
of object require each element to be instantiated
and inserted into the array... - Monster arraynew Monster2
- array0new Monster()
- array1new Monster()
- array0.take_damage()
8Loops and Arrays
- loops are natural ways to perform the same
operation on each element of an array (or a sub
set). For example - int arraynew int10
- int index
- for (index0indexlt10indexindex1)
-
- arrayindex0
-
9Loops and Dangers
- int arraynew int10
- int index
- for (index0indexlt10indexindex1)
-
- arrayindex0
-
- There is a problem with this code. Suppose I
change the array to only have 5 elements... as
soon as the index is 5 and tries to assign 0 to
the element in position 5 Java will issue an
array out of bounds exception
10Finding the number of elements in an array
- Fortunately we can ask the array how long it is
using .length - int arraynew int10
- int index
- for (index0indexltarray.lengthindexindex1)
-
- arrayindex0
-
- Now we can make the array bigger or smaller and
the code will not crash and will initialise the
entire array
11Problems with Arrays I
- The single biggest problem with an array is that
it is not variable length. You have to declare
the length of the array as a constant and you can
not tag on extra elements - int arraynew array20
- array301234
array out of bounds exception!
12Problems with arrays II
- You also can not remove elements in an array even
if they are no longer needed. For instance
suppose I have an array of 4 Monsters which the
Hero is fighting. If the Hero kills 2 it would be
nice to remove two from the array... the only
thing you can do is to replace the dead instance
with null.
13Strings
14Strings
- The String class allows you to store and
manipulate sequences of characters - Like chars, Strings are case sensitive so hello
is not the same as Hello - Strings are in fact Objects
- The length method tells you the length of a
string in chars e.g. hello.length()5 - You can get a copy of the character at any
position in a string using charAt() - hello.charAt(0)h
- hello.charAt(4)o
15Relationship between String and Arrays
- It is possible to get a copy of the contents of a
String as a char array - String s"hello world"
-
- char connew chars.length()
-
- s.getChars(0,s.length(),con,0)
-
make sure the array is large enough to store the
entire String
Starting char in the String
ending char 1 in the String
starting position in the array
array to copy chars into
16Relationship between String and Arrays
- It is also possible to create a string from a
char array - String s"hello world"
- char connew chars.length()
- s.getChars(0,s.length(),con,0)
- con0'H'
- snew String(con)
- System.out.println(s)
- However, manipulating char arrays is not easy so
in general it is better to manipulate the String
by keeping it in its String format
17Manipulating Strings
- There are many manipulation functions defined for
the string object one of the most useful is
subString - You can get a copy of part of a string using
subString - hello.subString(0,2)hel
- hello.substring(2,3)ll
ending index
starting index
18Adding to your String
- Unlike arrays, Strings can be appended and
pre-pended to easily - String sworld
- shello s
- ss!
- This gives the String hello world!
19Seeing if Two Strings are Equal using
- For simple types you can use and ! to see if
something is equal or not equal. - and ! do not work correctly for objects
- String shello there
- String s1hello there
- ss is true
- s1s1 is true
- ss1 is false!!!!!!!!!!!!!!
- This is because for objects and ! check where
the instance is in memory and not the contents of
the instances
20Seeing if Two Strings are Equal using equals
- To see if two Strings are identical you need to
use equals() or equalsIgnoreCase() - String shello there
- String s1hello there
- s.equals(s) is true
- s.equals(s1) is true
- s.equals(s1) is true
- This is because equals and equalsIgnoreCase check
the contents of the instances and not their
locations in memory
21Converting other types into Strings
- Java will convert almost any simple type into a
String for you - String s
- int i166
- si
- This does not work for Objects because Java can
not work out how to convert an object into a
String
is an empty String
converts a copy of i into a String
22Converting Strings into other types.
- Java provides static methods in classes called
Integer, Long, Float and Double to help you - int iInteger.parseInt("100")
- long lLong.parseLong("100")
- float fFloat.parseFloat("100")
- double dDouble.parseDouble("100")
- If and only if the entire String looks like
another type can the conversion take place. - int iInteger.parseInt(hello 100")
Exception
23More String Functions
- There are many more String manipulation functions
than we have time for to look at here - Take a look at the Java documentation for the
String class and have some fun!
24Vectors
25Vectors
- Vectors offer an alternative from arrays that
allow you to insert, delete and add extra
elements. - Unfortunately Vectors can only store objects so
you can not create a Vector of simple data types
such as int.... but there is nothing stopping you
putting a single int into a class and putting
that class into the Vector... - Vectors are in the package java.util and so
require importing - import java.util.
26Vectors
- Unlike arrays that can only store a single class
and its subclasses Vectors can store anything in
any position... Its up to you to remember what is
in each element... - Vector vnew Vector()
- v.insertElementAt(new Monster(),0)
- v.insertElementAt(new Hero(),1)
27Vectors getting the value of an element
- Unlike arrays elementAt is used to access an
element of the Vector - However, element at returns a generic Object
which must be type cast to be used - Vector vnew Vector()
- v.insertElementAt(new Monster(),0)
- Monster m(Monster)v.elementAt(0)
- m.take_damage()
28Vectors and invalid type casts
- If you use elementAt and type cast into the wrong
type of object you will get a invalid type cast
exception - Vector vnew Vector()
- v.insertElementAt(new Hero(),0)
- Monster m(Monster)v.elementAt(0)
- m.take_damage()
29Finding the number of elements in a Vector
- The vector is actually a class and we can ask it
how many elements are in it using the size method - Vector vnew Vector()
- v.insertElementAt(new Hero(),0)
- v.insertElementAt(new Dragon(),1)
- int iv.size()
30Removing items from Vectors
- You can remove a single element from the Vector
(all the high numbered elements are moved down to
fill the gap) - Vector vnew Vector()
- v.insertElementAt(new Hero(),0)
- v.insertElementAt(new Dragon(),1)
- v.removeElementAt(0)
- The elementAt(0) is now an instance of Dragon
31Removing All the items from a Vector
- You can remove all the items from a vector
- Vector vnew Vector()
- v.insertElementAt(new Hero(),0)
- v.insertElementAt(new Dragon(),1)
- v.removeAllElements()