Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By Junky
  • 1 Post By Junky

Thread: passing by ref issue

  1. #1
    BobswYourUcle is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default passing by ref issue

    Hello,

    I am new to passing by reference and I am not sure I am doing this the right way.

    I have the below loop, i want to add a list of fileList to the list FileGroup within the loop. However when I add the list
    Java Code:
    groupList.addAll(fileList)
    and then clear the list with
    Java Code:
    fileList.clear()
    so I can continue the loop (creating more file groups) I loose the file list I just added to the group list, leaving me with nothing.

    To fix this I created a method addFileList that just adds the list, as seen below.

    My question is is this an acceptable way to do this? or is this just plain wrong?

    Java Code:
    ...
     List<FileGroup> groupList = new ArrayList<FileGroup>();
     List<FileInfo> fileList = new ArrayList<FileInfo>();
     
     for(int i = 0; i < files.size(); i ++)
     { 
    	if(validateAdditionalfiles(files.get(i-1), files.get(i))) //if valid add to the list
    	{
    		fileList.add(files.get(i));
    	 }
    	 else
    	 {	//when invalid add the current file list to a group and keep going
    //		groupList.add(new FileGroup(fileList)); //I did it like this first
    		addfileList(groupList, fileList); //Now i use this menthos
    		fileList.clear(); //then i clear the list so I can continue
    		 
    		if(validateFirstFlight(minsUntilDeparture))
    		{
    			fileList.add(files.get(i));
    		}	
    	}
    }
    	
    private static void addFileList(List<FileGroup> groupList, List<FlightInfo> fileList) 
    {
    	 List<fileList> tmpList = new ArrayList<fileList>();
    
    	 tmpList.addAll(fileList);
    	 groupList.add(new FileGroup(tmpList));
    }
    Thanks

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java does not pass by reference. Java does pass by value. No ifs, buts or maybes. When you pass an object reference as a parameter then you pass that reference by value. It is important that you get that clear in your head.

    As to your problem, you can have a List of Lists. Or you can create a Foo class that holds a list of objects and then create a List of Foo objects. The advantage of the second option is that you can add helper methods to the class to add, remove search etc the List.
    JeffGrigg likes this.

  3. #3
    BobswYourUcle is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Thanks,

    Could you please give me a small example of how I would do this with a list of list? I am new to lists, and well java..

    Thanks again.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java Code:
    List<List<FileInfo>> list = ....
    JeffGrigg likes this.

  5. #5
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    I think it would help if you got your code to the point where it compiles first.

    For example, the method name on this line:
    Java Code:
    				addfileList(groupList, fileList); // Now i use this menthos
    needs to match this line exactly:
    Java Code:
    	private static void addFileList(List<FileGroup> groupList, List<FlightInfo> fileList) {
    Then you probably need to decide if the 'fileList' variable (and parameter) is a List of FileInfo, or a List of FlightInfo.

    This line probably contains error(s):
    Java Code:
    		List<fileList> tmpList = new ArrayList<fileList>();
    Now once you get it to compile, and maybe to run, I suspect that it might make more sense for the FileGroup class to contain a List of things than to have a List that directly contains another List.

  6. #6
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    I used to do some programming in C and C++ and from my understanding passing by reference means passing by memory location.

    Since Jave has a JRE with a garbage collector, passing by reference is not needed. You simply pass the variable directly.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    Instead of faffing about with this:
    Java Code:
    		addfileList(groupList, fileList); //Now i use this menthos
    		fileList.clear(); //then i clear the list so I can continue
    just create a new list each time, since that's what you want:
    Java Code:
    fileList = new ArrayList<FileInfo>();

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

    Default

    Quote Originally Posted by stchman View Post
    Since Jave has a JRE with a garbage collector, passing by reference is not needed. You simply pass the variable directly.
    What has the garbage collector to do with this?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Passing a value..
    By Lagarto in forum New To Java
    Replies: 3
    Last Post: 01-23-2011, 05:58 PM
  2. passing value
    By KumbhaniMehul in forum Java Servlet
    Replies: 1
    Last Post: 04-14-2010, 09:52 AM
  3. Passing Value
    By katherine_93 in forum New To Java
    Replies: 1
    Last Post: 03-08-2010, 05:56 AM
  4. Passing value....
    By casid in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-03-2010, 11:19 AM
  5. passing something
    By dinosoep in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-05-2009, 09:26 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
  •