Results 1 to 9 of 9

Thread: ArrayList help

  1. #1
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default ArrayList help

    I am kind of lost when it comes to arraylists so I am hoping maybe someone here could kind of explain them to me and like where and how they could be put to use.

    Also, I am a bit confused about how to initialize an array list

    all I know how to do is get the size of it which is just the array name .size()

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Are you familiar with an array? If so, an array list is simply a class which manages an array for you. Normally an array has a fixed size, but an array list is able to shrink and grow as you add and remove items. When you add an item to the array list, it more or less, checks the size of the underlying array and it sees if it can fit in the array, if it can, it adds it to the array. If it can't fit, it creates a new array that is larger, copies the old array into the new, adds the item, and re-assigns the underlying array.

    Here is a quick code example(without generics) of what it looks like
    Java Code:
    public class ArrayList{
      private int count;
      private Object[] under;
    
      //constructor initializes instance variables.
      public void add(Object o){
        if(count >= under.length){
          Object[] x = new Object[under.length + (under.length / 2)];
          for(int i = 0; i < under.length; ++i){
            x[i] = under[i];
            under = x;
          }
        }
        under[count++] = o;
      }
    }
    All the other methods look very similar to this(not exact, this is an example). If you want to see the exact code you can find it in your jdk folder under src.

    The brackets are called generics. In reality doesn't manage an Object array, instead it manages a T array, where T is whatever is passed in between the <>. It's quite simple to use an array list, it basically looks like this
    Java Code:
    ArrayList<Integer> aofint = ArrayList<Integer>(); //initialized to take only integers
    aofint.add(1);
    aofint.add(2);
    There are also methods to remove, get, and many other things, I suggest you check the API for more information on methods. ArrayList (Java Platform SE 6)

  3. #3
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    So if I had to do something like:
    The code below outlines a shell for a method called everyOtherLine with one parameter called
    fileName. This method should modify the file specified by fileName by removing every other line.
    Fill out the method with Java code to make it perform correctly. You will need to read in from the file
    and write back every other line, starting with the first. You must handle file not found and general exceptions. You may assume that any required packages have been imported. Remember, you need
    to modify the original file, not create a new one.
    Java Code:
    public void everyOtherLine(String fileName)
    {
    //since I have to use an arraylist I am assuming I have to set that up first by doing something like
    ArrayList<?Dont know what goes here> line = new ArrayList<>();
    }
    How would I proceed after that? kind of lost on this.
    I know I have to do a buffered reader and and a buffered writer, but don't know how those would really work.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As to how to read a file I would suggest you check out: File (Java Platform SE 6)

    Otherwise yet, you would declare an array list of strings. Then read a line in at a time and store it in the array list. The only bad thing about ths is you are storing the whole file in memory and you are doing x reads , then x writes. It may be easier to just bypass the array list and read and write as you go through the loop.

    A counter(int) can help you know which files to write.

  5. #5
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    on the question it says hint: use arraylists, so I am assuming I have to.
    But I am also extremely confused with buffered readers and io exceptions. I don't know when they are necessary. For every time you use a buffered reader or writer are they necessary?
    would this be kind of right...
    Java Code:
    public void everyOtherLine(String fileName)
    {
      String line = null;
      try
      {
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        while((line = reader.readLine()) != null)
         {
           for(int i = 2; i < lines.size(); i++)
           {
             if(i%2 == 0) //<=== the == 0 over here I don't understand but I know it goes there, so some explaining would be nice.
             {
               BufferedWriter writer = new BufferedWriter(BufferedWriter(fileName);
             }
           }
         }
      }
    }
    is that good? I don't really know if I'm doing it right.
    If you could maybe continue to help me out a bit with some commenting so I actually know what this stuff does instead of just writing it out

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I actually gave you the wrong link, meant to give you tutorials, but I gave you the API instead: Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

    If your teacher wants you to use an array list it's better to do that.
    Java Code:
    declare array list
    open stream
    loop
      read line into array list
    end loop
    open out stream
    loop
      write every other item in array list to stream
    end loop

  7. #7
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    so what I understand from the instructions is that I have to be able to take a file, remove every other line and then trim it to size so it writes it back looking normal, but it would write out every line.
    i.e.
    it would take:
    Java Code:
    Hi
    How
    Are
    You
    and write it back as
    Java Code:
    Hi
    Are
    so, I am assuming I am going to have to take the arrayList and do .trim(); after I removed every otherline. But how would I do that?

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Have you worked out how to store the file in an array list? If so, just loop through the array and call string on each item, then write if necessary.

    Java Code:
    loop
      if on even index, declare string, initialize to trimmed value
      write string to file
      else   //odd index
      skip
    end loop
    I hope the above helps.

  9. #9
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by adjit View Post
    so what I understand from the instructions is that I have to be able to take a file, remove every other line and then trim it to size so it writes it back looking normal, but it would write out every line.
    i.e.
    it would take:
    Java Code:
    Hi
    How
    Are
    You
    and write it back as
    Java Code:
    Hi
    Are
    so, I am assuming I am going to have to take the arrayList and do .trim(); after I removed every otherline. But how would I do that?
    Java Code:
    for(int i = 0; i < arrayList.size(); i=i+2;) {
    System.out.println(arrayList.get(i).trim());
    System.out.println();
    }
    The above code will skip an index of the ArrayList every iteration, since you're skipping an index when you add 2 to i. (let's say i is 3, if I add 2 to i then it will equal 5, meaning I skipped 4)

Similar Threads

  1. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 07:07 AM
  2. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 12:48 PM
  3. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  4. Replies: 2
    Last Post: 11-16-2010, 06:51 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 PM

Posting Permissions

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