0

i was wondering how to create a copy and return its object without using the clone function.

    public double[] Mean(double[][] data) {

    double[] x = data[0].clone();
  • why not use clone? – ItamarG3 Nov 08 '16 at 10:27
  • Please help me. – Prince of Persia Nov 08 '16 at 10:28
  • @PrinceofPersia please answer the question .... also, what do you want to copy, arrays or Object – AxelH Nov 08 '16 at 10:30
  • Dear @PrinceofPersia - show a bit of patience. You see, first of all, your question is kinda strange. clone works nicely for the example you provided; so dont push us on providing a solution to a strange question ... – GhostCat Nov 08 '16 at 10:31
  • Use this topic hope you can get all details [clonning](http://stackoverflow.com/questions/2156120/java-recommended-solution-for-deep-cloning-copying-an-instance) – FaigB Nov 08 '16 at 10:33
  • @PrinceofPersia - did you manage to get it working? – ItamarG3 Nov 08 '16 at 10:34
  • yh thanks everybody for partcipating – Prince of Persia Nov 08 '16 at 10:36
  • Sorry someone game me -1 thats y i was impatient – Prince of Persia Nov 08 '16 at 10:36
  • 1
    @PrinceofPersia probably because your question is a bit broad. Don't forget to accept the answer that help you the most. – AxelH Nov 08 '16 at 10:38
  • @PrinceofPersia if you think an answer is good, you should accept it (the `v` under the answer score). that way others know the answer works – ItamarG3 Nov 08 '16 at 10:38
  • ok it says i have to wait 5 min – Prince of Persia Nov 08 '16 at 10:39
  • @PrinceofPersia wait, I'm confused. Which answer did you choose? it's showing me both of them as accepted [o.o] – ItamarG3 Nov 08 '16 at 10:44
  • they both helped me – Prince of Persia Nov 08 '16 at 10:45
  • but which helped **more**? – ItamarG3 Nov 08 '16 at 10:45
  • @PrinceofPersia I might add that `Arrays` does it much more efficiently – ItamarG3 Nov 08 '16 at 10:46
  • Equally, they both helped probably Mark – Prince of Persia Nov 08 '16 at 10:47
  • @PrinceofPersia you misunderstand me. there's more than one way of doing almost anything in Java, but there are much fewer *right* ways. cloning an object is done via `.clone()` which you didn't want. the second `'right'` way of copying an `array` is using `Arrays.copyOf()`. using a for `loop` is a brute-force method which is less efficient in terms of runtime than `Arrays.copyOf()`... – ItamarG3 Nov 08 '16 at 10:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127620/discussion-between-itamar-green-and-prince-of-persia). – ItamarG3 Nov 08 '16 at 10:53
  • He wanted to know how it worked. If someone wants to know how to build a car without having the leading expert build it for him you don't give him the address of yet another expert who can build a car for him, you show him how it's done. Sure, brute force is slower, but it's also very simple to understand, which was the point. But honestly this discussion is way off topic, so Prince just choose Itamars as the correct answer, we'll leave the other up too and everyone is happy. – Mark Nov 08 '16 at 11:32

2 Answers2

4

You can use this:

double copy = Arrays.copyOf(data[0],data[0].length);

Which will, as the name states, return a copy of the array. NOTE: this will only work for arrays, since it's a method from java.util.Arrays (self-explanatory)

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
2
public double[] colMean(double[][] data) {
    double[] x = new double[data[0].length];
    for (int i = 0; i < x.length; i++) {
        x[i] = data[0][i];
    }
    return x;
}

Odd that you only want to copy data[0] though, but whatever...

Mark
  • 1,498
  • 1
  • 9
  • 13