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();
}
}
java arrays
|
show 4 more comments
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();
}
}
java arrays
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
|
show 4 more comments
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();
}
}
java arrays
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
java arrays
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
|
show 4 more comments
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
|
show 4 more comments
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);
}
add a comment |
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
But great minds think alike 1+ to this answer
– DontKnowMuchBut Getting Better
Nov 7 at 21:36
add a comment |
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);
}
add a comment |
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);
}
add a comment |
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);
}
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);
}
answered Nov 7 at 21:31
DontKnowMuchBut Getting Better
2,1631512
2,1631512
add a comment |
add a comment |
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
But great minds think alike 1+ to this answer
– DontKnowMuchBut Getting Better
Nov 7 at 21:36
add a comment |
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
But great minds think alike 1+ to this answer
– DontKnowMuchBut Getting Better
Nov 7 at 21:36
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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