Results 1 to 11 of 11
Thread: Array List
- 12-20-2011, 08:07 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
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));
}
}
- 12-20-2011, 08:10 PM #2
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!
- 12-20-2011, 08:12 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
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
- 12-20-2011, 08:14 PM #4
Re: Array List
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-20-2011, 08:16 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
Re: Array List
It says identifier expected (box=new ArrayList(); )
- 12-20-2011, 08:31 PM #6
Re: Array List
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-20-2011, 08:35 PM #7
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
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());
}
}
- 12-20-2011, 08:40 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,403
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-20-2011, 08:44 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
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));
}
}
- 12-20-2011, 08:47 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,403
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java Code:ArrayList box= new ArrayList();
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-20-2011, 08:48 PM #11
Member
- Join Date
- Dec 2011
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Linked List, Array List time complexity
By Rick99771977 in forum New To JavaReplies: 4Last Post: 08-18-2011, 05:37 AM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
Array List help
By Weejee37 in forum New To JavaReplies: 4Last Post: 10-27-2009, 12:32 AM -
Array List
By mashyum in forum Advanced JavaReplies: 2Last Post: 07-28-2009, 06:47 AM -
using an Array list
By toad in forum New To JavaReplies: 1Last Post: 11-18-2007, 09:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks