Results 1 to 17 of 17
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default array list COPYING...SO CONFUSED

    I have a class Fish
    Java Code:
    public Fish(Fish x) { //copy constuctor
    
    		x = new Fish(x.getRow(),x.getCol(), x.getSize(), x.getDirection());
    I have this method I have to implement.
    Java Code:
    /** Returns a COPY of the fish list.  Hint:  Use the ArrayList<Fish> copy 
    	 * constructor, or you will fail our tests! */
    	
               public ArrayList<Fish> getFish() {
    		throw new RuntimeException("You code this!");
    	}
    here's my attempt (Im not familiar with syntax)
    Java Code:
    /** Returns a COPY of the fish list.  Hint:  Use the ArrayList<Fish> copy 
    	 * constructor, or you will fail our tests! */
    	public ArrayList<Fish> getFish() {
    	
    
    ArrayList<Fish> newArr;
    newArr = new ArrayList<fish>(Fish x);
    
    	}
    Thanks for tips, hints and help.

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

    Default Re: array list COPYING...SO CONFUSED

    since you have to use the first method stated above i would do:

    Something like this, it may not be perfect its difficult for me to picture this without running the code and such.

    Java Code:
    public ArrayList<Fish> getFish() {
           ArrayList<Fish> copy = new ArrayList<Fish>();
    
         for(Fish f: oldfish){
         Fish x = new Fish(f);
         copy.add(x);
        }
    return copy;
    }

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: array list COPYING...SO CONFUSED

    thanks. How do I reference copy a 2D array in general?

  4. #4
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: array list COPYING...SO CONFUSED

    SOrry. I made a mistake.

    Here is my copy constructor.

    I have a class called Model and it holds (numRows, numCols, numRocks, numFish, numPlants)

    I have a method with a parameter named other of type Model.

    I want to copy three things

    1. int[][] landscape (2D array holding row and col)
    2. ArrayList of type Fish called fish.
    2. ArrayList of type Plant called plants.


    Java Code:
    /** Copy Constructor.
    	 * Since landscape is immutable in the scope of our project, you could
    	 *   do a simple reference copy for it.
    	 * However, Fish and Plants are mutable, so those lists must be copied 
    	 *   with a DEEP copy!
    	 * (In other words, each fish and each plant must be copied.)
    	 */
    
    public Model(Model other) {
    
            		
                    int[][] copy = new int [other.getRows()][other.getCols()];
    		
    		
                    for (Fish f : fish){
    			other.addFish(f);
    		}
    		for (Plant p : plants){
    			other.addPlant(p);
    		}
    
    	}

    Just making sure I did the reference copy and two deep copies right.

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

    Default Re: array list COPYING...SO CONFUSED

    there are several ways to copy 2d arrays. There's cloning, nested loops etc etc. :)

    one example:

    Java Code:
    int [][] copy = new int[landscape.length][];
    for(int i = 0; i < landscape.length; i++)
    copy[i] = landscape[i].clone()

  6. #6
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: array list COPYING...SO CONFUSED

    I just need to reference copy it.

    Are my deep copies right for arraylist?
    I am using the for each statemnt

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

    Default Re: array list COPYING...SO CONFUSED

    see my above comment, re-posting in case you missed it since you posted during my post edit.

    Try your code, thats the only way you will learn if you copies are correct or not :)

  8. #8
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: array list COPYING...SO CONFUSED

    Is there a way that does not involve clone method? landscape is a 2D array with immutable objects so do I still need to loop through and set each element via a for-loop?

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

    Default Re: array list COPYING...SO CONFUSED

    i would yes, there is possibly other ways to do this though. There are many ways that dont involve clone, are you not allowed to use clone?

    Clone creates a reference copy.

  10. #10
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: array list COPYING...SO CONFUSED

    I cant use clone. I just need to reference copy.

    this is what i have:
    private ArrayList<Fish> fish;
    private ArrayList<Plant> plants;
    private int[][] landscape;


    ------------

    In a copy constructor i am making im confused.

    1. landscape is a 2D array type int and its object is immutable..so it can never be changed..so i just need to reference copy that
    2. plants and fish are arraylists but have mutable objects..so i need to DEEP copy...

    QUESTION: Am i reference copying and deep copying right?
    I AM REALLY SORRY FOR BEING ANNOYING , DONT HATE ME.

    so heres my copy constructor:


    Java Code:
    /** Copy Constructor.
    	 * Since landscape is immutable in the scope of our project, you could
    	 *   do a simple reference copy for it.
    	 * However, Fish and Plants are mutable, so those lists must be copied 
    	 *   with a DEEP copy!
    	 * (In other words, each fish and each plant must be copied.)
    	 */
    	public Model(Model other) {
    
    		
    		int[][] copy = new int [other.getRows()][other.getCols()];  //am i referencing copying this right?
    		
    		fish = new ArrayList<Fish>();   //am i DEEP copying this right?
    		for (Fish f : other.fish){
    			fish.add(f);
    		}
    			
    	    plants = new ArrayList<Plant>();  //am I DEEP copying this right?
    		for (Plant p : other.plants){ 
    			plants.add(p);
    		}
    
    	}

    SORRY FOR BEING REPTITVE..PLEASE DONT HATE ME AND THANK YOU!!!

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

    Default Re: array list COPYING...SO CONFUSED

    you are not deep copying the arrayList

    You need to add this line and call your copy constructor:

    Fish x = new Fish(f);
    copy.add(x);

    Otherwise you are just creating reference copy.

  12. #12
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: array list COPYING...SO CONFUSED

    THank you for putting up with me. Ill try it out and post revised code soon

  13. #13
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: array list COPYING...SO CONFUSED

    For this question please ignore my first post its completely wrong. that previous post im posting is the copy constructor. I was wondering if my constructor is ok? WIthout the constructor I cant proceed to actually copying.

  14. #14
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: array list COPYING...SO CONFUSED

    I use the wrong copy constructor. I am acutally building this constructor that will be used for the original question...Im so sorry for being confusing.

  15. #15
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: array list COPYING...SO CONFUSED

    alrighty i fixed it...I have a new question:

    here's my copy constructor :

    Java Code:
    	public Model(Model other) {
    
    		landscape = other.landscape;
    
    		fish = new ArrayList<Fish>();
    		for (Fish f : other.fish) {
    			fish.add(new Fish(f));
    		}
    
    		plants = new ArrayList<Plant>();
    		for (Plant p : other.plants){
    			plants.add(new Plant(p));
    		}
    	}

    How do I call my copy constructor when implementing this method:

    Java Code:
    /** Returns a COPY of the plants list.  Hint:  Use the ArrayList<Plant> 
    	 * copy constructor, or you will fail our tests! */ 
    	public ArrayList<Plant> getPlants() {
    		
    //copy of the plants list
    
    	}
    Thanks for your continued help and support

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: array list COPYING...SO CONFUSED

    First off, if the Fish constructor in your original post is the one you are using to deep copy a Fish then it is wrong.
    It makes no sense, so you probably need to revisit that.

    As for your getPlants() method, is that a normal ArrayList (as in java.util.ArrayList)?
    If so, then just return a new ArrayList, passing in the plants list.
    If it is not the standard ArrayList then you'll need to show us what the one you are using looks like.
    Please do not ask for code as refusal often offends.

  17. #17
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: array list COPYING...SO CONFUSED

    I figured it out. Thanks!

Similar Threads

  1. Copying elements in an array
    By Blondedude092 in forum New To Java
    Replies: 7
    Last Post: 11-04-2012, 10:34 PM
  2. copying a linked list
    By Get_tanked in forum New To Java
    Replies: 17
    Last Post: 02-22-2011, 02:06 AM
  3. Trouble copying an array
    By xXRedneckXx in forum New To Java
    Replies: 10
    Last Post: 02-05-2011, 05:36 PM
  4. Copying ArrayList into an Array
    By Manfizy in forum New To Java
    Replies: 6
    Last Post: 07-16-2009, 07:03 AM
  5. deep copying arraylist to add to a list
    By alvations in forum New To Java
    Replies: 13
    Last Post: 10-08-2008, 03:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •