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

    Default HOW to reference a copy of an array

    Hi. I was wondering how to reference a copy of an array.

    I know how to copy an array.

    I would make a new array of original array.length, and populate
    the array using a for-loop going through it.

    I dont know how to reference that copy.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,110
    Rep Power
    10

    Default Re: HOW to reference a copy of an array

    How do you reference the original array?

    I suggest creating an SSCCE that demonstrates exactly what you've tried and where you're stuck.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

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

    Default Re: HOW to reference a copy of an array

    Im working on a rather large project so I cant really show an example like that. Ill try to provide the same concept. I want to know how to reference a copy of an array.

    Code segment:

    Java Code:
    private String[] arr;
    
    //some method tht instantiates it
    arr = new String [3];
    
    //some method that copies 
    
    String[] new_arr = new String[arr.length]
    
    for (int i = 0; i < arr.length; i++){
       new_arr[i] = arr[i]
    
    }
    
    //some method that asks to return a reference of a copy of an array
    
    
    //im not sure how reference a copied array

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,351
    Rep Power
    7

    Default Re: HOW to reference a copy of an array

    Perhaps this is what you mean. Assuming an array of type String.

    Java Code:
    public String[] copyArray(String [] src) {
         String [] dst = new String[src.length];
    // copy code here
         return dst;
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,168
    Rep Power
    4

    Default Re: HOW to reference a copy of an array

    I would use the Arrays utility class:
    Java Code:
    String[] foo = {"a", "b", "c"};
    String[] copy = Arrays.copyOf(foo, foo.length);

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,110
    Rep Power
    10

    Default Re: HOW to reference a copy of an array

    Quote Originally Posted by NoobieCode View Post
    Im working on a rather large project so I cant really show an example like that. Ill try to provide the same concept. I want to know how to reference a copy of an array.

    Code segment:

    Java Code:
    private String[] arr;
    
    //some method tht instantiates it
    arr = new String [3];
    
    //some method that copies 
    
    String[] new_arr = new String[arr.length]
    
    for (int i = 0; i < arr.length; i++){
       new_arr[i] = arr[i]
    
    }
    
    //some method that asks to return a reference of a copy of an array
    
    
    //im not sure how reference a copied array
    You're already referencing both arrays.

    Recommended reading: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Reference an array so it can be accessed
    By DGates in forum New To Java
    Replies: 5
    Last Post: 02-06-2012, 10:12 PM
  2. no array reference
    By droidus in forum New To Java
    Replies: 12
    Last Post: 10-05-2011, 05:05 AM
  3. Array copy
    By lakshmibvaraprasad in forum New To Java
    Replies: 16
    Last Post: 07-21-2011, 09:17 PM
  4. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  5. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 12:56 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
  •