Results 1 to 7 of 7
Thread: Using ArrayList as a parameter
- 05-12-2010, 08:00 AM #1
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
Using ArrayList as a parameter
hey guys, so ive done this before but it doesnt seem to be working and i have no idea why. heres the code ive got:
public static void main(String[] args)
{
ArrayList<Collection> collections = new ArrayList<Collection>();
}
public static void selectionChoice(String selection)
{...some code not causing problem...
if(selection.equals("P"))
printListDetails(collections);
else if(selection.equals("E"))
listEmpty(collections);
public static void printListDetails(ArrayList<Collection> collections)
{...more code
public static boolean listEmpty(ArrayList<Collection> collections)
{...more code
of course, ive got a class called Collection with a constructor and various methods etc.
but when i compile it says
cannot find symbol
symbol: variable collections
location class ...
printListDetails(collections);
with a ^ pointing to collections
why cant i allow to use the arraylist as a parameter?
- 05-12-2010, 08:14 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
If you declare something nside a method then it's only available for your use inside that method only. You can't use it outside that method.
You declared collections inside the main method and are now trying to use it in a different method called selectionChoice.
- 05-12-2010, 09:23 AM #3
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
ahk i understand that now,
but for my program there is only ever one arraylist, is there some way in which i can declare the arraylist in one of the classes so when i change methods and that the arraylist contents will always be the same?
- 05-12-2010, 09:31 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
If you declare it outside any method in a class then it becomes available to all the methods in the class depending on the static status.
P.S You have made all your methods static. This is usually a sign of bad design except when making utility methods in utility classes.
- 05-13-2010, 04:22 AM #5
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
sorry i dont quite understand. so i should make a new method that just declares the arraylist. something like
public ArrayList<Collection> collections = new ArrayList<Collection>();
or should it be private?
and then make all methods static or non static?
- 05-13-2010, 06:34 AM #6
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
hope it helpsJava Code:public class Test1 { public ArrayList<Collection> collections //making it //available to all methods if you declare outside the methods but inside the class public Test1() { ArrayList<Collection> collection= new ArrayList<Collection>(); } public void method1() { you can use it collections here.... } public void method2() { you can use it collections here too.... } ....... public static void main(....) { Test1 t = new Test1(); } }
- 05-13-2010, 06:56 AM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
@Crypts: See my response above again. You only make methods static if they are utility methods not when they are actions peformed by an object that you are trying to model.
@[RaiIdEn]
The arraylist you declare and initialize inside that constructor is only available within that constructor. You should also not make instance variables public. A possible improvement of your code is
Java Code:public class Test { //making it available to all methods if you declare outside the methods but inside the class private ArrayList<Collection> collection; public Test() { //Don't declare here, just initialize collection= new ArrayList<Collection>(); } public void method1() { //you can use it collections here.... } public void method2() { //you can use it collections here too.... } public static void main(String ...) { Test t = new Test();//t's collection is now initialized t.method1();//e.t.c } }
Similar Threads
-
Subclass name to be a parameter?
By Peetahzee in forum New To JavaReplies: 6Last Post: 12-12-2009, 03:51 PM -
Can a method take itself as parameter?
By bukake in forum New To JavaReplies: 10Last Post: 09-06-2008, 09:26 PM -
Retrieving a parameter through URL.
By hisouka in forum Java ServletReplies: 1Last Post: 09-06-2008, 12:45 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 -
arugment/parameter
By ravian in forum New To JavaReplies: 5Last Post: 01-04-2008, 09:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks