Merging a new object in java with an already existing one











up vote
-2
down vote

favorite












I want to know how can i merge two objects in java. I've tried creating a 3rd class but to no avail. The first one being the this object and the next one is given through the method. Something like:



import java.util.Arrays;

public final class IntegerArray {
private int a;

public IntegerArray(int a) {
this.a = a;
}

public int length() {
return a.length;
}

public int getElementAt(int i) {
return a[i];
}

public int sum() {
int sum = 0;
for(int i: a) {
sum += i;
}
return sum;
}

public double average() {
int i, sum = 0, armean;
for(i = 0; i < a.length; i++) {
sum = sum + a[i];
}
armean = sum / i;
return armean;
}

public IntegerArray getSorted() {
int b = a.clone();
Arrays.sort(b);
return new IntegerArray(b);
}

public IntegerArray contact(IntegerArray ia) {
IntegerArray merged = new IntegerArray(this.a);
}

@Override
public String toString() {
return a.toString();
}


}









share|improve this question




















  • 1




    please show some codes and what you have done so far.
    – Kick Buttowski
    Nov 7 at 20:54










  • Are you trying to merge two objects or combine the elements from two different arrays? Not sure what you're trying to accomplish here.
    – Zephyr
    Nov 7 at 20:56












  • It is not clear what you mean by "merge"?
    – QBrute
    Nov 7 at 20:56










  • There is no generic way to merge all type of objects. You have to write that functionality yourself. Keep in mind that for some types (for example Collection#add All) this does already exist.
    – Glains
    Nov 7 at 20:57






  • 1




    The problem is, from what you've provided, we don't know what you're trying to accomplish. Maybe include some of your code and indicate the portion you're stuck on. What are your input values and what do you expect to get back in return?
    – Zephyr
    Nov 7 at 21:02















up vote
-2
down vote

favorite












I want to know how can i merge two objects in java. I've tried creating a 3rd class but to no avail. The first one being the this object and the next one is given through the method. Something like:



import java.util.Arrays;

public final class IntegerArray {
private int a;

public IntegerArray(int a) {
this.a = a;
}

public int length() {
return a.length;
}

public int getElementAt(int i) {
return a[i];
}

public int sum() {
int sum = 0;
for(int i: a) {
sum += i;
}
return sum;
}

public double average() {
int i, sum = 0, armean;
for(i = 0; i < a.length; i++) {
sum = sum + a[i];
}
armean = sum / i;
return armean;
}

public IntegerArray getSorted() {
int b = a.clone();
Arrays.sort(b);
return new IntegerArray(b);
}

public IntegerArray contact(IntegerArray ia) {
IntegerArray merged = new IntegerArray(this.a);
}

@Override
public String toString() {
return a.toString();
}


}









share|improve this question




















  • 1




    please show some codes and what you have done so far.
    – Kick Buttowski
    Nov 7 at 20:54










  • Are you trying to merge two objects or combine the elements from two different arrays? Not sure what you're trying to accomplish here.
    – Zephyr
    Nov 7 at 20:56












  • It is not clear what you mean by "merge"?
    – QBrute
    Nov 7 at 20:56










  • There is no generic way to merge all type of objects. You have to write that functionality yourself. Keep in mind that for some types (for example Collection#add All) this does already exist.
    – Glains
    Nov 7 at 20:57






  • 1




    The problem is, from what you've provided, we don't know what you're trying to accomplish. Maybe include some of your code and indicate the portion you're stuck on. What are your input values and what do you expect to get back in return?
    – Zephyr
    Nov 7 at 21:02













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I want to know how can i merge two objects in java. I've tried creating a 3rd class but to no avail. The first one being the this object and the next one is given through the method. Something like:



import java.util.Arrays;

public final class IntegerArray {
private int a;

public IntegerArray(int a) {
this.a = a;
}

public int length() {
return a.length;
}

public int getElementAt(int i) {
return a[i];
}

public int sum() {
int sum = 0;
for(int i: a) {
sum += i;
}
return sum;
}

public double average() {
int i, sum = 0, armean;
for(i = 0; i < a.length; i++) {
sum = sum + a[i];
}
armean = sum / i;
return armean;
}

public IntegerArray getSorted() {
int b = a.clone();
Arrays.sort(b);
return new IntegerArray(b);
}

public IntegerArray contact(IntegerArray ia) {
IntegerArray merged = new IntegerArray(this.a);
}

@Override
public String toString() {
return a.toString();
}


}









share|improve this question















I want to know how can i merge two objects in java. I've tried creating a 3rd class but to no avail. The first one being the this object and the next one is given through the method. Something like:



import java.util.Arrays;

public final class IntegerArray {
private int a;

public IntegerArray(int a) {
this.a = a;
}

public int length() {
return a.length;
}

public int getElementAt(int i) {
return a[i];
}

public int sum() {
int sum = 0;
for(int i: a) {
sum += i;
}
return sum;
}

public double average() {
int i, sum = 0, armean;
for(i = 0; i < a.length; i++) {
sum = sum + a[i];
}
armean = sum / i;
return armean;
}

public IntegerArray getSorted() {
int b = a.clone();
Arrays.sort(b);
return new IntegerArray(b);
}

public IntegerArray contact(IntegerArray ia) {
IntegerArray merged = new IntegerArray(this.a);
}

@Override
public String toString() {
return a.toString();
}


}






java arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 21:03

























asked Nov 7 at 20:52









Stefan

225




225








  • 1




    please show some codes and what you have done so far.
    – Kick Buttowski
    Nov 7 at 20:54










  • Are you trying to merge two objects or combine the elements from two different arrays? Not sure what you're trying to accomplish here.
    – Zephyr
    Nov 7 at 20:56












  • It is not clear what you mean by "merge"?
    – QBrute
    Nov 7 at 20:56










  • There is no generic way to merge all type of objects. You have to write that functionality yourself. Keep in mind that for some types (for example Collection#add All) this does already exist.
    – Glains
    Nov 7 at 20:57






  • 1




    The problem is, from what you've provided, we don't know what you're trying to accomplish. Maybe include some of your code and indicate the portion you're stuck on. What are your input values and what do you expect to get back in return?
    – Zephyr
    Nov 7 at 21:02














  • 1




    please show some codes and what you have done so far.
    – Kick Buttowski
    Nov 7 at 20:54










  • Are you trying to merge two objects or combine the elements from two different arrays? Not sure what you're trying to accomplish here.
    – Zephyr
    Nov 7 at 20:56












  • It is not clear what you mean by "merge"?
    – QBrute
    Nov 7 at 20:56










  • There is no generic way to merge all type of objects. You have to write that functionality yourself. Keep in mind that for some types (for example Collection#add All) this does already exist.
    – Glains
    Nov 7 at 20:57






  • 1




    The problem is, from what you've provided, we don't know what you're trying to accomplish. Maybe include some of your code and indicate the portion you're stuck on. What are your input values and what do you expect to get back in return?
    – Zephyr
    Nov 7 at 21:02








1




1




please show some codes and what you have done so far.
– Kick Buttowski
Nov 7 at 20:54




please show some codes and what you have done so far.
– Kick Buttowski
Nov 7 at 20:54












Are you trying to merge two objects or combine the elements from two different arrays? Not sure what you're trying to accomplish here.
– Zephyr
Nov 7 at 20:56






Are you trying to merge two objects or combine the elements from two different arrays? Not sure what you're trying to accomplish here.
– Zephyr
Nov 7 at 20:56














It is not clear what you mean by "merge"?
– QBrute
Nov 7 at 20:56




It is not clear what you mean by "merge"?
– QBrute
Nov 7 at 20:56












There is no generic way to merge all type of objects. You have to write that functionality yourself. Keep in mind that for some types (for example Collection#add All) this does already exist.
– Glains
Nov 7 at 20:57




There is no generic way to merge all type of objects. You have to write that functionality yourself. Keep in mind that for some types (for example Collection#add All) this does already exist.
– Glains
Nov 7 at 20:57




1




1




The problem is, from what you've provided, we don't know what you're trying to accomplish. Maybe include some of your code and indicate the portion you're stuck on. What are your input values and what do you expect to get back in return?
– Zephyr
Nov 7 at 21:02




The problem is, from what you've provided, we don't know what you're trying to accomplish. Maybe include some of your code and indicate the portion you're stuck on. What are your input values and what do you expect to get back in return?
– Zephyr
Nov 7 at 21:02












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










In your merge method, you need to create a new int, one whose length is the size of the this.a.length + the other object's length, use for loops to place the this.a values into the new array, and then another for-loop to place the merging object's array's values in. Note that you must be careful to use the correct indices for the new array when adding inthe 2nd array -- you must add the first int array's length to the index when referencing the new array item. Then create a new IntegerArray object with this newly created longer array.



public IntegerArray merge(IntegerArray other) {
int newA = new int[a.length + other.a.length];
for (int i = 0; i < a.length; i++) {
newA[i] = a[i];
}
for (int i = 0; i < other.a.length; i++) {
// here is where you need to be careful about the index
newA[i + a.length] = other.a[i];
}
return new IntegerArray(newA);
}





share|improve this answer




























    up vote
    1
    down vote













    In the contact() method, you need to create an array whose length is equal to the sum of the lengths of the this.a and ia, arrays then copy the this.a array into that new array and also copy the contents of ia into it. Then you can create a new IntegerArray passing that new array as the argument to the constructor.



      int temp = new int[sum_of_lengths];
    // copy this.a elements into temp
    // copy ia elements into temp
    IntegerArray merged = new IntegerArray(temp);


    Aah - @DontKnowMuchBut beat me by a few seconds






    share|improve this answer





















    • But great minds think alike 1+ to this answer
      – DontKnowMuchBut Getting Better
      Nov 7 at 21:36













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197633%2fmerging-a-new-object-in-java-with-an-already-existing-one%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    In your merge method, you need to create a new int, one whose length is the size of the this.a.length + the other object's length, use for loops to place the this.a values into the new array, and then another for-loop to place the merging object's array's values in. Note that you must be careful to use the correct indices for the new array when adding inthe 2nd array -- you must add the first int array's length to the index when referencing the new array item. Then create a new IntegerArray object with this newly created longer array.



    public IntegerArray merge(IntegerArray other) {
    int newA = new int[a.length + other.a.length];
    for (int i = 0; i < a.length; i++) {
    newA[i] = a[i];
    }
    for (int i = 0; i < other.a.length; i++) {
    // here is where you need to be careful about the index
    newA[i + a.length] = other.a[i];
    }
    return new IntegerArray(newA);
    }





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      In your merge method, you need to create a new int, one whose length is the size of the this.a.length + the other object's length, use for loops to place the this.a values into the new array, and then another for-loop to place the merging object's array's values in. Note that you must be careful to use the correct indices for the new array when adding inthe 2nd array -- you must add the first int array's length to the index when referencing the new array item. Then create a new IntegerArray object with this newly created longer array.



      public IntegerArray merge(IntegerArray other) {
      int newA = new int[a.length + other.a.length];
      for (int i = 0; i < a.length; i++) {
      newA[i] = a[i];
      }
      for (int i = 0; i < other.a.length; i++) {
      // here is where you need to be careful about the index
      newA[i + a.length] = other.a[i];
      }
      return new IntegerArray(newA);
      }





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        In your merge method, you need to create a new int, one whose length is the size of the this.a.length + the other object's length, use for loops to place the this.a values into the new array, and then another for-loop to place the merging object's array's values in. Note that you must be careful to use the correct indices for the new array when adding inthe 2nd array -- you must add the first int array's length to the index when referencing the new array item. Then create a new IntegerArray object with this newly created longer array.



        public IntegerArray merge(IntegerArray other) {
        int newA = new int[a.length + other.a.length];
        for (int i = 0; i < a.length; i++) {
        newA[i] = a[i];
        }
        for (int i = 0; i < other.a.length; i++) {
        // here is where you need to be careful about the index
        newA[i + a.length] = other.a[i];
        }
        return new IntegerArray(newA);
        }





        share|improve this answer












        In your merge method, you need to create a new int, one whose length is the size of the this.a.length + the other object's length, use for loops to place the this.a values into the new array, and then another for-loop to place the merging object's array's values in. Note that you must be careful to use the correct indices for the new array when adding inthe 2nd array -- you must add the first int array's length to the index when referencing the new array item. Then create a new IntegerArray object with this newly created longer array.



        public IntegerArray merge(IntegerArray other) {
        int newA = new int[a.length + other.a.length];
        for (int i = 0; i < a.length; i++) {
        newA[i] = a[i];
        }
        for (int i = 0; i < other.a.length; i++) {
        // here is where you need to be careful about the index
        newA[i + a.length] = other.a[i];
        }
        return new IntegerArray(newA);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 21:31









        DontKnowMuchBut Getting Better

        2,1631512




        2,1631512
























            up vote
            1
            down vote













            In the contact() method, you need to create an array whose length is equal to the sum of the lengths of the this.a and ia, arrays then copy the this.a array into that new array and also copy the contents of ia into it. Then you can create a new IntegerArray passing that new array as the argument to the constructor.



              int temp = new int[sum_of_lengths];
            // copy this.a elements into temp
            // copy ia elements into temp
            IntegerArray merged = new IntegerArray(temp);


            Aah - @DontKnowMuchBut beat me by a few seconds






            share|improve this answer





















            • But great minds think alike 1+ to this answer
              – DontKnowMuchBut Getting Better
              Nov 7 at 21:36

















            up vote
            1
            down vote













            In the contact() method, you need to create an array whose length is equal to the sum of the lengths of the this.a and ia, arrays then copy the this.a array into that new array and also copy the contents of ia into it. Then you can create a new IntegerArray passing that new array as the argument to the constructor.



              int temp = new int[sum_of_lengths];
            // copy this.a elements into temp
            // copy ia elements into temp
            IntegerArray merged = new IntegerArray(temp);


            Aah - @DontKnowMuchBut beat me by a few seconds






            share|improve this answer





















            • But great minds think alike 1+ to this answer
              – DontKnowMuchBut Getting Better
              Nov 7 at 21:36















            up vote
            1
            down vote










            up vote
            1
            down vote









            In the contact() method, you need to create an array whose length is equal to the sum of the lengths of the this.a and ia, arrays then copy the this.a array into that new array and also copy the contents of ia into it. Then you can create a new IntegerArray passing that new array as the argument to the constructor.



              int temp = new int[sum_of_lengths];
            // copy this.a elements into temp
            // copy ia elements into temp
            IntegerArray merged = new IntegerArray(temp);


            Aah - @DontKnowMuchBut beat me by a few seconds






            share|improve this answer












            In the contact() method, you need to create an array whose length is equal to the sum of the lengths of the this.a and ia, arrays then copy the this.a array into that new array and also copy the contents of ia into it. Then you can create a new IntegerArray passing that new array as the argument to the constructor.



              int temp = new int[sum_of_lengths];
            // copy this.a elements into temp
            // copy ia elements into temp
            IntegerArray merged = new IntegerArray(temp);


            Aah - @DontKnowMuchBut beat me by a few seconds







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 7 at 21:33









            FredK

            3,000149




            3,000149












            • But great minds think alike 1+ to this answer
              – DontKnowMuchBut Getting Better
              Nov 7 at 21:36




















            • But great minds think alike 1+ to this answer
              – DontKnowMuchBut Getting Better
              Nov 7 at 21:36


















            But great minds think alike 1+ to this answer
            – DontKnowMuchBut Getting Better
            Nov 7 at 21:36






            But great minds think alike 1+ to this answer
            – DontKnowMuchBut Getting Better
            Nov 7 at 21:36




















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197633%2fmerging-a-new-object-in-java-with-an-already-existing-one%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()