Results 1 to 2 of 2
Thread: Linked List integer list
- 12-10-2008, 01:08 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
Linked List integer list
I have a file with a list of unordered integers that I want to read to a linked list. I'll be using Linked lists in an upcoming project and have no experience with them so thought I'd get my hands dirty with this first. I have this code (slightly changed) working with strings to make a list, but I want to use it with integers, putting each one into the list to they are in ascending order.
Can anyone point me in the right direction to split this into simple methods to read in one line at a time ordering as it goes? (I know how to do this with arrays, vectors and ArrayList)PHP Code:// Make Input/Output classes available import java.io.*; import java.util.*; class OrderedList { // Assign the file specified as the first argument specified in the command-line as 'filename' public void getData(String filename) { try { // Open the file specified as the first argument specified in the command-line FileInputStream fstream = new FileInputStream(filename); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String fileLine; // change to int or convert after to int? // Create a list to store the file data List<Integer> list = new LinkedList<Integer>(); // Read the file line by line and write to list while ((fileLine = br.readLine()) != null) { list.add(fileLine); } in.close(); for (int i = 0; i < 13; i++) { System.out.println(list.get(i)); } } // Catch exceptions catch(ArrayIndexOutOfBoundsException e) { } catch(Exception e) { System.err.println("Error: " + e.getMessage()); } } // Close getFileData() public static void main(String args[]) { try { OrderedList draw = new OrderedList(); // Pass first command-line argument to getFileData method draw.getData(args[0]); } catch () {} } // Close main() } // Close class
- 12-10-2008, 08:53 PM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
I think you're confusing two different problems.
(1) If at the END of the whole process you want to have a sorted list, then just read ALL your values in, and THEN call Collections.sort() once on the whole list (in terms of what method you call, it makes no difference whether it's an ArrayList, Vector or LinkedList-- that's the beauty of object-oriented languages...!)
(2) If your poblem is that you want to know how to MAINTAIN a permanently sorted list of things when you add items at arbitrary points in time, then:
- Each time you add an item, you want to add it "in the right place" to maintain order: the Collections.binarySearch() method will tell you the correct place to insert an item in a list to keep the list sorted
- but LinkedList isn't a good choice for this, because inserting at an arbitrary point in a linked list is slow; use an ArrayList instead
By the way, at some point, it's worth learning the correct idiom for reading lines from a file. You don't need the DataInputStream, and you should call close() in a finally block on the BufferedReader (the "last component in the chain" that's reading from the stream).Neil Coffey
Javamex - Java tutorials and performance info
Similar Threads
-
Linked List removeAll Help
By unc123w in forum New To JavaReplies: 13Last Post: 09-30-2008, 02:41 PM -
Linked List help
By neobie in forum New To JavaReplies: 8Last Post: 12-22-2007, 03:15 AM -
going from vectors to linked list?
By cbrown08 in forum New To JavaReplies: 3Last Post: 12-01-2007, 12:55 AM -
Linked List
By rnavarro9 in forum New To JavaReplies: 0Last Post: 11-29-2007, 03:42 AM -
Help with linked list
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks