Results 1 to 7 of 7
  1. #1
    Crypts is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default 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?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  3. #3
    Crypts is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    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?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  5. #5
    Crypts is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    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?

  6. #6
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Java 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();
        }
    }
    hope it helps

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    @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

  1. Subclass name to be a parameter?
    By Peetahzee in forum New To Java
    Replies: 6
    Last Post: 12-12-2009, 03:51 PM
  2. Can a method take itself as parameter?
    By bukake in forum New To Java
    Replies: 10
    Last Post: 09-06-2008, 09:26 PM
  3. Retrieving a parameter through URL.
    By hisouka in forum Java Servlet
    Replies: 1
    Last Post: 09-06-2008, 12:45 PM
  4. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  5. arugment/parameter
    By ravian in forum New To Java
    Replies: 5
    Last Post: 01-04-2008, 09:43 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
  •