Results 1 to 16 of 16
  1. #1
    cjtemple is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default clear of ArrayList has unexpected result

    I have a couple arraylists that I fill up and then use to create a new object and then clear the lists to reuse again. However the values within the object are getting cleared as well when I call the clear for the list. If I comment out the clear statements than my object is unaffected (well not completely unaffected, the values I intend to add to the r2 object are added to r1).


    Java Code:
    	public void loadFile(boolean full) {
    
    		_rotor_in = new ArrayList<Integer>();
    		_rotor_out = new ArrayList<Integer>();
    		
    		String[] rotor01, rotor02, rotor03, rotor04;
    		if(!full) {
    			rotor01 = _context.getResources().getStringArray(R.array.r01_standard);
    			rotor02 = _context.getResources().getStringArray(R.array.r02_standard);
    		} else {
    			rotor01 = _context.getResources().getStringArray(R.array.r01_extended);
    			rotor02 = _context.getResources().getStringArray(R.array.r02_extended);
    		}
    		for(int i = 0; i < rotor01.length; i++ ){
    			String[] items = rotor01[i].split(",");
    			_rotor_in.add(Integer.parseInt(items[0]));
    			_rotor_out.add(Integer.parseInt(items[1]));
    		}
    		r1 = new Rotor(_rotor_in, _rotor_out);
    		_rotor_in.clear();
    		_rotor_out.clear();
    		for(int i = 0; i < rotor02.length; i++ ){
    			String[] items = rotor02[i].split(",");
    			_rotor_in.add(Integer.parseInt(items[0]));
    			_rotor_out.add(Integer.parseInt(items[1]));
    		}
    		r2 = new Rotor(_rotor_in, _rotor_out);
    }
    Rotor class:
    Java Code:
    public class Rotor implements Transcoder {
    
    	private ArrayList<Integer> _input, _output;
    	
    	public Rotor(ArrayList<Integer> i, ArrayList<Integer> o) {
    		this._input = i;
    		this._output = o;
    	}
    			
    	@Override
    	public Integer getTranscode(Integer currentValue) {
    		return this._output.indexOf(this._input.get(currentValue));
    	}
    
    }
    Last edited by cjtemple; 10-18-2013 at 02:50 AM.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: clear of ArrayList has unexpected result

    if you clear a list, the elements in it will obviously get cleared, if you want to reuse the list and maintain the elements in the list, i suggest creating a new list and before you clear copy the list elements over to the storage list.

  3. #3
    cjtemple is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: clear of ArrayList has unexpected result

    Oh wait it is a passByReference and not a passByValue issue isn't it?

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: clear of ArrayList has unexpected result

    think so, easy way to check:

    open your debugger and check the memory address of what your working that will tell you if its pass by value or reference

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,760
    Rep Power
    5

    Default Re: clear of ArrayList has unexpected result

    All methods in Java are pass by value. Even if you pass a reference, the value of the reference is passed, not the address of the reference. So you can change what the reference refers to, but not the value of the reference itself.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    cjtemple is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: clear of ArrayList has unexpected result

    Jim,

    That kind of confuses me as I did as monkey suggested and the address are the same.

    clear of ArrayList has unexpected result-screen-shot-2013-10-17-10.07.20-pm.png

    perhaps since this is on an adroid device the memory handling is different

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,760
    Rep Power
    5

    Default Re: clear of ArrayList has unexpected result

    Well, I am not an Android programmer. But here is a code fragment that demonstrates my understanding of pass by reference (aka pass by name).

    int a = 10;
    public void changeA(int b) {
    b = 20;
    }
    Now a = 20.

    The same would also be true if a were an object. If the changeA method were to assign a new object directly to 'b', then 'a' would also be
    affected. But Java does not work that way. The only language I ever programmed in that worked that way is Fortran IV. I believe
    Algol 60 also has pass by reference.

    Edit: If you are passing an array or a reference to a List object, that is still pass by value. But you can still wipe out what the array or list refers to,
    just not the value of the array or list reference itself.

    Regards,
    Jim
    Last edited by jim829; 10-18-2013 at 04:31 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,760
    Rep Power
    5

    Default Re: clear of ArrayList has unexpected result

    One more thing. Clearing an array or List should only remove those elements from the array or list. It should not affect the fields of the elements you are removing.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: clear of ArrayList has unexpected result

    Java Code:
    r1 = new Rotor(_rotor_in, _rotor_out); 
    ...
    r2 = new Rotor(_rotor_in, _rotor_out);
    Of course they're the same.
    You've stuck the same two references in each one, so the _input and _output attributes will hold the same values.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,760
    Rep Power
    5

    Default Re: clear of ArrayList has unexpected result

    After I had logged off for the evening I realized I got off on a tangent and didn't even address the OP's original question.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    cjtemple is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: clear of ArrayList has unexpected result

    It seams I have something off than. My intent is to read in a file, go through that file and store the values in a variable. Then I would created a new object with the values from the file. I would next like to store a new value, from another file, to the original variable (without effecting the object that was just created), and then create a new object with the new value. The code above shows the logic, but clearly isn't correct in practice :)

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: clear of ArrayList has unexpected result

    Don't do it all in one method then.
    The method you have up there is clearly doing too much, so break it down, and then you won't have this problem.

    In general I find that if you find yourself writing a method that has some variables that you end up reusing for some other task then you probably want to break the method down a bit.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    cjtemple is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: clear of ArrayList has unexpected result

    Tolls,

    You are absolutely correct. Right now I am just stubbing out the logic, and shortly I will be optimizing the code. I was able to work out my issue as my constructor was just creating a remote control to the original values (reference borrowed from JavaRanch Campfire - Pass By Value, Please). I changed the constructor to clone the arraylist and all is well

    Java Code:
    	public Rotor(ArrayList<Integer> i, ArrayList<Integer> o) {
    		this._input = (ArrayList<Integer>) i.clone();
    		this._output = (ArrayList<Integer>) o.clone();
    	}

  14. #14
    cjtemple is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: clear of ArrayList has unexpected result

    Thanks for the help, I have it working and cleaned up a bit. Just the dreaded documenting next :)

    Java Code:
    	public void loadFile(boolean full) {
    		_rotor_in = new ArrayList<Integer>();
    		_rotor_out = new ArrayList<Integer>();
    		
    		for(int i = 1; i <=4; i++ ) {
    			Integer rsArrayID;
    			if(!full) {
    				rsArrayID = _context.getResources().getIdentifier("r0"+i+"_standard", "array", "com.templetek.enigma");
    			} else {
    				rsArrayID = _context.getResources().getIdentifier("r0"+i+"_extended", "array", "com.templetek.enigma");
    			}
    			String[] rotor = _context.getResources().getStringArray(rsArrayID);
    			for(int j = 0; j < rotor.length; j++) {
    				String[] items = rotor[j].split(",");
    				_rotor_in.add(Integer.parseInt(items[0]));
    				_rotor_out.add(Integer.parseInt(items[1]));
    			}
    			switch (i) {
    			case 1:
    				r1 = new Rotor(_rotor_in, _rotor_out);
    				break;
    			case 2:
    				r2 = new Rotor(_rotor_in, _rotor_out);
    				break;
    			case 3:
    				r3 = new Rotor(_rotor_in, _rotor_out);
    				break;
    			case 4:
    				r4 = new Rotor(_rotor_in, _rotor_out);
    				break;
    			}
    			_rotor_in.clear();
    			_rotor_out.clear();
    		}
    	}

  15. #15
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: clear of ArrayList has unexpected result

    You know you could save the whole cloning thing by simply declaring the _rotor_in and_rotor_out variables inside the for loop.
    They're not needed outside the loop and they look like they should be local variables and not member variables.

    That would allow you to remove the clone call and the clear call.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    cjtemple is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: clear of ArrayList has unexpected result

    Again, you are correct :)
    Those are up there from a previous iteration before I subclassed this down to Rotor.

Similar Threads

  1. Unexpected behavior
    By suhaas.mohandos@gmail.com in forum New To Java
    Replies: 9
    Last Post: 08-19-2013, 09:20 AM
  2. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  3. Struts 2 error : No result defined for action / result
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2011, 10:15 AM
  4. Problem arraylist result when use Collections.sort
    By i4ba1 in forum Advanced Java
    Replies: 4
    Last Post: 02-09-2011, 08:00 AM
  5. unexpected type
    By one in forum New To Java
    Replies: 13
    Last Post: 01-20-2009, 09:32 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
  •