Results 1 to 11 of 11

Thread: Array List

  1. #1
    turtlish is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Array List

    Hello!Sorry to bother you again! I'm trying to make an Array list and I want the user to have the ability to modify the elements.When I'm compiling this programm,the compiler can't identify the "box" ( 7 line source code).What I'm doing wrong? Thanks very much!


    Here is the source code:
    import java.util.*;
    public class TicketPool
    { public static void TicketPool(){
    ArrayList box;
    box=new ArrayList();}

    public static void AddTickets() {

    box.add(new Ticket()); //cannot find symbol

    }

    public static void DisplayElements(){
    ArrayList box;
    box=new ArrayList();
    box.add(new Ticket());
    for(int i=0; i<box.size(); i++)
    System.out.println(box.get(i));
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Array List

    Where do you declare box? If it's inside a method, that's the only place it will be available. If you declare two different variables both named box in two different methods, those are two completely different values.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    turtlish is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Array List

    Can I declare a box outside a method so it can be the same for the class? I'm really sorry maybe all these questions sounds to stupid, but I'm really new and confused to Java

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Array List

    Quote Originally Posted by turtlish View Post
    Can I declare a box outside a method so it can be the same for the class? I'm really sorry maybe all these questions sounds to stupid, but I'm really new and confused to Java
    What happened when you tried?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    turtlish is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Array List

    It says identifier expected (box=new ArrayList(); )

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Array List

    Quote Originally Posted by turtlish View Post
    It says identifier expected (box=new ArrayList(); )
    I don't see a declaration there, only an attempted instantiation.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    turtlish is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Array List

    This is how I did it.I wrote only the parenthesis because that was the line that the error comes up

    import java.util.*;
    public class TicketPool{
    ArrayList box;
    box=new ArrayList();


    public static void AddTickets() {

    box.add(new Ticket());

    }
    }

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array List

    Please show us the complete (relevant) code because what you've shown us in reply #7 doesn't match with what you showed us in your original post.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    turtlish is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Array List

    Hello! This is code (while I'm trying to make the array list outside a method)
    import java.util.*;
    public class TicketPool{
    ArrayList box;
    box=new ArrayList();


    public static void AddTickets() {

    box.add(new Ticket());

    }

    public static void DisplayElements(){

    for(int i=0; i<box.size(); i++)
    System.out.println(box.get(i));
    }
    }

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array List

    You can't have ordinary statements (or expressions) outside of a method body; change the first two lines in your class to one single line to make it an initializer declaration:

    Java Code:
    ArrayList box= new ArrayList();
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    turtlish is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

Similar Threads

  1. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  2. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  3. Array List help
    By Weejee37 in forum New To Java
    Replies: 4
    Last Post: 10-27-2009, 12:32 AM
  4. Array List
    By mashyum in forum Advanced Java
    Replies: 2
    Last Post: 07-28-2009, 06:47 AM
  5. using an Array list
    By toad in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 09:08 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
  •