Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Array in constructor and deep copy.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    EU - Bladefist Alliance
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array in constructor and deep copy.

    Hello, this is an school homework task, studying over the net and i've got none to ask in my hometown or friends. only gaming wow and bf3.

    This is the start of one class and i'm having troubble with it.

    1. the arrays seems to be not right. (seems = is)
    2. deep copy, the right array shall be one copy of the left array

    class shoes{
    	private final String brand;
    	private final String colour;
    	private final double price;
    	private final int minSize;
    	private final int[] left;
    	private final int[] right;
     
    	public shoes(String brand, String colour, double price, int minSize, int[] left, int[] right) {
    		this.brand = brand;
    		this.colour = colour;
    		this.price = price;
    		this.minSize = minSize;
    		this.left = new int[] left;
    		this.right = new int[] right;   // thisone shall be one deep copy of the left array.
    	}
    }

    I'm not asking for one complete solution, would be nice but i've been trying to search on google for tips. and my school book are of no use either on their examples.

    is there some example code or web site i could look on ?

    regards


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Location
    EU - Bladefist Alliance
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array in constructor and deep copy.

    think i'm on my move:

    	public shoes(String brand, String colour, double price, int minSize, int[] left, int[] right) {
    		this.brand = brand;
    		this.colour = colour;
    		this.price = price;
    		this.minSize = minSize;
    		this.left = new int[] left;
    		for(int i=0;i<left.length;i++){
    			right[i] = left[i];
    		}
    		this.right = new int[] right;   // thisone shall be one deep copy of the left array.
    	}

    but still getting error message.

    G:\Coding\Skole\Øving 9\tabeller.java:20: error: array dimension missing
    		this.left = new int[] left;
    		                      ^
    G:\Coding\Skole\Øving 9\tabeller.java:24: error: array dimension missing
    		this.right = new int[] right;   // thisone shall be one deep copy of the left array.
    		                       ^
    2 errors
     
    Tool completed with exit code 1

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Array in constructor and deep copy.

    See the following for the correct syntax in declaring arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics) - you must define the size of the array when you create it.
    To copy the arrays, you need to loop over one and place its values in the second (as your second post demonstrates).

Similar Threads

  1. Copy Constructor
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 7th, 2021, 09:12 PM
  2. Deep copy of ArrayList?
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2011, 01:30 PM
  3. Constructor doesn't fill in array properly
    By Whyareall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 6th, 2010, 04:24 AM
  4. [SOLVED] Array of objects, invoking constructor for one changes others
    By BigFoot13 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 24th, 2010, 01:30 PM
  5. Object array with constructor
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2010, 11:02 AM

Tags for this Thread