OK, please post questions/comments related to today's class in the Comments.
I wanted to correct one thing I said in class about Interfaces.
When you assign to a variable whose type is an Interface name, you've got to make it refer to an actual object, and every object is an instance of an actual Class. So, to initialize a List<String> variable, you can't write:
List<String> myList = new List<String> ( );
You'll get a compile time error, pointing out that there is no such constructor because List is an interface, not a class. Instead, do something like:
List<String> myList = new ArrayList<String> ( ); // or
I wanted to correct one thing I said in class about Interfaces.
When you assign to a variable whose type is an Interface name, you've got to make it refer to an actual object, and every object is an instance of an actual Class. So, to initialize a List<String> variable, you can't write:
List<String> myList = new List<String> ( );
You'll get a compile time error, pointing out that there is no such constructor because List is an interface, not a class. Instead, do something like:
List<String> myList = new ArrayList<String> ( ); // or
List<String> myList = new LinkedList<String> ( );
Again, when the type of a variable is an Interface, the variable (if non-null) will always refer to an instance of some implementing class.
Here's an exercise to think about:
Write a List class called SilentArrayList that acts like ArrayList except:
If the user tries to set or get a value that is out of range (>= this.size( )), instead of throwing an exception, it just silently fails, returning a null reference in the get case.
There are some technical issues involving the type of item to be stored, but try not to get too hung up about those for now, even though they need to be handled right if you want your solution to compile.
Hint: You can extend ArrayList to make your life easier.
ReplyDeletewhat other uses are there for list<> besides being a better replacement for array[]
ReplyDeleteBurton, I'm not sure what you mean. It is a better replacement for arrays, in a few different ways. Not much more to it. Perhaps a better question would be: what are the important differences between List and whatever[]?
DeleteThis comment has been removed by the author.
ReplyDeleteEDIT: Fixed problems in original code.
ReplyDeleteHere is my attempt at the exercise: http://pastebin.com/1UVgF7ya
Here is my test code for the exercise: http://pastebin.com/cpgwD20j
The tricky part was discovering the difference between size and capacity in ArrayList's implementation, and how that difference is a functional one when dealing with add, set, and get. Notice in my testing file that I add elements first before I play with get() or set(). I do this because ArrayList must have elements added before they can be set, even though the ArrayList has a capcity of 10 initially.
It is really cool that you can call the function that you are overriding in the function that you are overriding it with with the super.function() call. Very interesting, nice work man!
DeleteTo make this not return a null exception can't you just overwrite the get() methods so that if the element that is wanting to be added into the arrayList is out of range, instead of throwing the exception it returns null?
ReplyDeleteThis explains it. I was trying this kind of thing:
ReplyDeleteList myList = new List ( );
I'll make sure to use LinkedList or ArrayList.
Thanks!