Results 1 to 6 of 6
  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Question Array vs ArrayList

    I am busy with a program that reads in a file in an arrayList and then reads a 2nd file and looks if there are sentences beginning with a word that is in the arrayList.

    But now I was wondering.
    What can I use better?
    An array or an arrayList?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    What do you mean by "better"?

    Does your current code work?
    Is it easy to understand?

  3. #3
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Found it.

    I decided to use an arraylist, only to find a small problem.

    I am reading in a file that contains 2 lines.

    >HSIBIYG
    >OHOUBO7

    But he only adds line 1 to the arrayList.

    Java Code:
     
    			// Create Scanner for file1.
    			Scanner x = new Scanner(file1);
    			// We read the lines one by one in the file.
    
    			// Keeps track of the count after something has been found.
    			int getal = 0;
    
    			// Count the number of lines.
    			if (getal > 0) {
    				getal++;
    			}
    
    			if (x.hasNextLine()) {
    				String line = x.nextLine();
    				// Het ingelezen bestand word geprint.
    				System.out.println(line);
    
    				// ++ adds +1 every line.
    				getal++;
    
    				// We put what is in file1 into an arrayList.
    				arrayList.add(line);
    				System.out.println(arrayList);
    
    			}

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    That has nothing to do with ArrayList and everything to do with the fact you are only adding one line to it.

    That's an 'if' statement, not a loop, so it'll only execute once.

  5. #5
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    I first had a while and not an if.
    But then he prints my other while loop 2 times.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Well, that code there will only read a single line, and therefore only add a single line to the arraylist.

Similar Threads

  1. 2D array of ArrayList (technically a 3D array)
    By Lil_Aziz1 in forum New To Java
    Replies: 3
    Last Post: 05-15-2010, 08:11 PM
  2. Array and arraylist
    By MIA6 in forum New To Java
    Replies: 3
    Last Post: 11-29-2009, 02:22 AM
  3. [SOLVED] Should I use an arraylist or an array?
    By matzahboy in forum New To Java
    Replies: 5
    Last Post: 11-17-2008, 04:34 AM
  4. ArrayList versus Array
    By junpogi in forum New To Java
    Replies: 13
    Last Post: 11-08-2008, 02:04 AM
  5. Array to ArrayList
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 12:46 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
  •