Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: ArrayList Troubles.

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default ArrayList Troubles.

    I just learned about ArrayLists from my book and how to implement them and such. Please tell me whats different (besides the ammount and names) of these two pieces of code:

    	ArrayList<String> coordsComp = new ArrayList<String>();
     
    	String space11 = "(1,1)";
    	String space12 = "(1,2)";
    	String space13 = "(1,3)";
    	String space21 = "(2,1)";
    	String space22 = "(2,2)";
    	String space23 = "(2,3)";
    	String space31 = "(3,1)";
    	String space32 = "(3,2)";
    	String space33 = "(3,3)";
     
    	coordsComp.add(space11);
    	coordsComp.add(space12);
    	coordsComp.add(space13);
    	coordsComp.add(space21);
    	coordsComp.add(space22);
    	coordsComp.add(space23);
    	coordsComp.add(space31);
    	coordsComp.add(space32);
    	coordsComp.add(space33);

    and

    		ArrayList<String> test = new ArrayList<String>();
     
    		String hi = "HI!";
    		String hi2 = "(1,2)";
     
    		test.add("Hi!");
    		test.add(hi);
    		test.add(hi2);
    		System.out.println("test: " +test);

    The problem i am having is that the top one won't compile but the bottom one will and i can't see the difference is. Please help! If you need to know the error i get says:

     
    compMove.java:17: <identifier> expected
    coordsComp.add(space11);
    ^
    compMove.java:17: <identifier> expected
    coordsComp.add(space11);
    ^
    compMove.java:18: <identifier> expected
    coordsComp.add(space12);
    ^
    compMove.java:18: <identifier> expected
    coordsComp.add(space12);
    ^
    compMove.java:19: <identifier> expected
    coordsComp.add(space13);
    ^
    compMove.java:19: <identifier> expected
    coordsComp.add(space13);
    ^
    compMove.java:20: <identifier> expected
    coordsComp.add(space21);
    ^
    compMove.java:20: <identifier> expected
    coordsComp.add(space21);
    ^
    compMove.java:21: <identifier> expected
    coordsComp.add(space22);
    ^
    compMove.java:21: <identifier> expected
    coordsComp.add(space22);
    ^
    compMove.java:22: <identifier> expected
    coordsComp.add(space23);
    ^
    compMove.java:22: <identifier> expected
    coordsComp.add(space23);
    ^
    compMove.java:23: <identifier> expected
    coordsComp.add(space31);
    ^
    compMove.java:23: <identifier> expected
    coordsComp.add(space31);
    ^
    compMove.java:24: <identifier> expected
    coordsComp.add(space32);
    ^
    compMove.java:24: <identifier> expected
    coordsComp.add(space32);
    ^
    compMove.java:25: <identifier> expected
    coordsComp.add(space33);
    ^
    compMove.java:25: <identifier> expected
    coordsComp.add(space33);
    ^
    18 errors
    Press any key to continue . . .



    The carrot points to right before and after space(#) not anywhere else
    Last edited by sp11k3t3ht3rd; November 2nd, 2010 at 09:47 AM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: ArrayList Troubles.

    It compiles fine for me?!

    	public static void main(String[] args) {
     
    		ArrayList<String> coordsComp = new ArrayList<String>();
     
    	    String space11 = "(1,1)";
    	    String space12 = "(1,2)";
    	    String space13 = "(1,3)";
    	    String space21 = "(2,1)";
    	    String space22 = "(2,2)";
    	    String space23 = "(2,3)";
    	    String space31 = "(3,1)";
    	    String space32 = "(3,2)";
    	    String space33 = "(3,3)";
     
    	    coordsComp.add(space11);
    	    coordsComp.add(space12);
    	    coordsComp.add(space13);
    	    coordsComp.add(space21);
    	    coordsComp.add(space22);
    	    coordsComp.add(space23);
    	    coordsComp.add(space31);
    	    coordsComp.add(space32);
    	    coordsComp.add(space33);
     
    	    System.out.println(coordsComp);
    	}

    Console output:

     

    [(1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3)]


    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    sp11k3t3ht3rd (November 2nd, 2010)

  4. #3
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: ArrayList Troubles.

    I was being an idiot and I forgot to put it into a method...

  5. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: ArrayList Troubles.

    Quote Originally Posted by sp11k3t3ht3rd View Post
    I was being an idiot and I forgot to put it into a method...
    Glad it's resolved. I have marked this thread as solved
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  3. JSP Troubles
    By sdkeslar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2010, 02:26 PM
  4. Array Troubles
    By Leeds_Champion in forum Collections and Generics
    Replies: 6
    Last Post: October 22nd, 2009, 11:05 AM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM