Java Reference
In-Depth Information
Lab 9.4: Using the LinkedList Class
The purpose of this lab is to become familiar with using a collection class
for the first time. The class you will use is java.util.LinkedList, which rep-
resents a linked list. A linked list is a list implementation in which each ele-
ment contains a reference to both the next and previous elements in the list.
1. Find the java.util.LinkedList class in the Java documentation. Look
over its constructors and methods.
2. Write a class named StringSorter. Add a field of type LinkedList and
initialize this field in the StringSorter constructor.
3. Add a method named addString() that has a String parameter. This
method should add the given String to the LinkedList, maintaining
the list in alphabetical order. You will need to search the list and
determine where in the list the String should appear.
4. Write a method named printList() that displays all the String objects
in the LinkedList.
5. Save and compile the StringSorter class.
6. Write a program named AddString that contains main(). Within
main(), instantiate a StringSorter object.
7. Add about a dozen Strings using the addString() method.
8. Print out the list using the printList() method.
9. Save, compile, and run the StringSorter program. Verify that it is
working properly.
Summary
An array is a collection of elements stored in a contiguous block of
memory. Arrays are fixed in length and cannot grow or shrink once
they are declared. Every array has a length attribute that contains the
size of the array.
■■
Arrays can be initialized using the new keyword or by using an array
initializer.
■■
Java allows for multidimensional arrays of any dimension.
■■
Search WWH ::




Custom Search