Results 1 to 8 of 8
Thread: passing by ref issue
- 08-12-2011, 12:27 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
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 listand then clear the list withJava Code:groupList.addAll(fileList)
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.Java Code:fileList.clear()
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?
ThanksJava 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)); }
- 08-12-2011, 12:33 AM #2
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.
- 08-12-2011, 01:00 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
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.
- 08-12-2011, 01:14 AM #4
Java Code:List<List<FileInfo>> list = ....
- 08-12-2011, 01:32 AM #5
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
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:
needs to match this line exactly:Java Code:addfileList(groupList, fileList); // Now i use this menthos
Then you probably need to decide if the 'fileList' variable (and parameter) is a List of FileInfo, or a List of FlightInfo.Java Code:private static void addFileList(List<FileGroup> groupList, List<FlightInfo> fileList) {
This line probably contains error(s):
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.Java Code:List<fileList> tmpList = new ArrayList<fileList>();
- 08-12-2011, 02:05 AM #6
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.
- 08-12-2011, 09:37 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Instead of faffing about with this:
just create a new list each time, since that's what you want:Java Code:addfileList(groupList, fileList); //Now i use this menthos fileList.clear(); //then i clear the list so I can continue
Java Code:fileList = new ArrayList<FileInfo>();
- 08-12-2011, 10:05 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Passing a value..
By Lagarto in forum New To JavaReplies: 3Last Post: 01-23-2011, 05:58 PM -
passing value
By KumbhaniMehul in forum Java ServletReplies: 1Last Post: 04-14-2010, 09:52 AM -
Passing Value
By katherine_93 in forum New To JavaReplies: 1Last Post: 03-08-2010, 05:56 AM -
Passing value....
By casid in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 01-03-2010, 11:19 AM -
passing something
By dinosoep in forum Threads and SynchronizationReplies: 2Last Post: 12-05-2009, 09:26 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks