Results 1 to 9 of 9
Thread: ArrayList help
- 05-08-2011, 10:48 PM #1
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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()
- 05-08-2011, 11:07 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
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.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; } }
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
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)Java Code:ArrayList<Integer> aofint = ArrayList<Integer>(); //initialized to take only integers aofint.add(1); aofint.add(2);
- 05-08-2011, 11:21 PM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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.
How would I proceed after that? kind of lost on this.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<>(); }
I know I have to do a buffered reader and and a buffered writer, but don't know how those would really work.
- 05-08-2011, 11:38 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 05-08-2011, 11:58 PM #5
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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...
is that good? I don't really know if I'm doing it 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); } } } } }
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
- 05-09-2011, 12:03 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
- 05-09-2011, 12:24 AM #7
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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:
and write it back asJava Code:Hi How Are You
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:Hi Are
- 05-09-2011, 02:02 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
I hope the above helps.Java Code:loop if on even index, declare string, initialize to trimmed value write string to file else //odd index skip end loop
- 05-09-2011, 02:07 AM #9
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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)Java Code:for(int i = 0; i < arrayList.size(); i=i+2;) { System.out.println(arrayList.get(i).trim()); System.out.println(); }
Similar Threads
-
copying contents of an ArrayList to another ArrayList
By ankit1801 in forum New To JavaReplies: 8Last Post: 03-27-2011, 06:07 AM -
sorting arraylist based on another arraylist
By busdude in forum New To JavaReplies: 4Last Post: 02-07-2011, 11:48 AM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
A private static ArrayList hold an object and static getter ArrayList
By Louis in forum New To JavaReplies: 2Last Post: 11-16-2010, 05:51 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks