Posted by: chilloxk on: January 31, 2009
The lab for monday is almost entirely the same as the DVD labs. Here I’ll post some minor bits we went over in lectures which might help a bit.
Switch Statements
These are handy for checking specific things like numbers’ equality. They are useful if you want to have menus and use numbers for imput and the like.
eg:
we have a menu that says ‘please enter a number to select an option’, which is stored in an int called choice:
println(“please enter a number to select an option: “);
int choice = readInt();
the syntac for switch is:
switch (expression){
case n:
method();
break;
case n+1:
method2();
break;
}
this checks if expression is equal to n, if not, n+1, and will keep going down the list. it will execute the methods inside the case: until it comes to break; which ends the statement.
SAVE / LOAD
as in the lectures:
save:
saveToFile(dvds, “DVDs.xml”);
in this case, what happens is the contents of dvds(which is an arrayList of type String) is saved in the file called DVDs.xml in the directory your project is saved in.
load:
dvds = (ArrayList<String>) readFromFile(“DVDs.xml”);
this overwrited the file dvds (which is an ArrayList<String>) with the contents of the file DVDs.xml in the project directory.
FOR:EACH LOOP
Watch out with these, in a for:each loop, you cycle through elements of an array, arraylist or whatever, but you cannot modify the contents of the thing you are cycling though. It actually gives you a copy of what is contained, not the file itself.
syntax: for(type anyname: listname)
type: String, int, DVD, Song.
anyname: the name which you will refer to each element by.
listname: the exact name of the array/ArrayList.
eg:
public void listDVDs(){
for(String moo: dvds)
{ println(dvd); }
}
This will pick the first element in the arrayList ‘dvds’ (which holds String names), which shall be refered to as moo, and then execute whatever is inside the curly brackets. in this case it prints them out.
If it held DVD types, this would print the names of the dvds:
public void listDVDs(){
for(DVD moo: dvds)
{ print( moo.getTitle() ); }
}
Because we would get each DVD object stored in the arrayList, and all its methods inside it too, like a normal for loop.
ITERATOR
Don’t entirely know much about these, just that they are ways of cycling through and altering elements in lists/sets. Similar to a for-loop but with it’s own methods.
import the use of it by using: import java.util.Iterator;
we create it by the following: (not sure on the capital or small ‘i’/'I’)
Iterator<Type> iteratorname = collection.iterator();
eg: Iterator<DVD> it = dvds.iterator();
this creates an iterator which can cycle through DVD objects, we will call it ‘it’, and it will be attached to the dvds arrayList collection.
this iterator has its own methods;
hasNext() – which returns a true/false if there is another element in the list.
and next() – which gets the next element.
used generally (for the moment) like this:
while(it.hasNext() )
{ it.next(); }
to print the name of the DVDs:
while(it.hasNext() )
{ println(it.next().getTitle() ); }
Next: HashSets and HastMaps
specific questions to chilloxk[-at-]hotmail(-dot-).COM