Results 1 to 2 of 2
  1. #1
    igniteflow is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Unhappy 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.

    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
    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)

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    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).

Similar Threads

  1. Linked List removeAll Help
    By unc123w in forum New To Java
    Replies: 13
    Last Post: 09-30-2008, 02:41 PM
  2. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 03:15 AM
  3. going from vectors to linked list?
    By cbrown08 in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 12:55 AM
  4. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 03:42 AM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •