Is it possible to copy some of an array's values into another array but with the same reference? [duplicate]
This question already has an answer here:
Grab a segment of an array in Java without creating a new array on heap
15 answers
I think the title is self-descriptive but let's just put an example
float shape1 = new float[4]; //Those 4 floats are XCoord,YCoord,Width,Height
float shape1XY = new float[2] /*I want to store here the values of the
first 2 floats from shape1
with the ability that when i change them
shape1[0] and shape1[1] are also changed */
Is there a way of actually doing this?
java arrays reference
marked as duplicate by dasblinkenlight
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 19:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Grab a segment of an array in Java without creating a new array on heap
15 answers
I think the title is self-descriptive but let's just put an example
float shape1 = new float[4]; //Those 4 floats are XCoord,YCoord,Width,Height
float shape1XY = new float[2] /*I want to store here the values of the
first 2 floats from shape1
with the ability that when i change them
shape1[0] and shape1[1] are also changed */
Is there a way of actually doing this?
java arrays reference
marked as duplicate by dasblinkenlight
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 19:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
@dasblinkenlight Sorry but...which answer there is the right one? can't really tell
– Omar Abdul'Azeez
Nov 11 at 20:02
There's no "right" answer to this question, as there is no way to do what you wish to do. Hence you need to pick a work-around that is the most appropriate, or the least annoying, in your circumstances.
– dasblinkenlight
Nov 11 at 20:09
add a comment |
This question already has an answer here:
Grab a segment of an array in Java without creating a new array on heap
15 answers
I think the title is self-descriptive but let's just put an example
float shape1 = new float[4]; //Those 4 floats are XCoord,YCoord,Width,Height
float shape1XY = new float[2] /*I want to store here the values of the
first 2 floats from shape1
with the ability that when i change them
shape1[0] and shape1[1] are also changed */
Is there a way of actually doing this?
java arrays reference
This question already has an answer here:
Grab a segment of an array in Java without creating a new array on heap
15 answers
I think the title is self-descriptive but let's just put an example
float shape1 = new float[4]; //Those 4 floats are XCoord,YCoord,Width,Height
float shape1XY = new float[2] /*I want to store here the values of the
first 2 floats from shape1
with the ability that when i change them
shape1[0] and shape1[1] are also changed */
Is there a way of actually doing this?
This question already has an answer here:
Grab a segment of an array in Java without creating a new array on heap
15 answers
java arrays reference
java arrays reference
asked Nov 11 at 19:10
Omar Abdul'Azeez
185
185
marked as duplicate by dasblinkenlight
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 19:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by dasblinkenlight
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 19:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
@dasblinkenlight Sorry but...which answer there is the right one? can't really tell
– Omar Abdul'Azeez
Nov 11 at 20:02
There's no "right" answer to this question, as there is no way to do what you wish to do. Hence you need to pick a work-around that is the most appropriate, or the least annoying, in your circumstances.
– dasblinkenlight
Nov 11 at 20:09
add a comment |
@dasblinkenlight Sorry but...which answer there is the right one? can't really tell
– Omar Abdul'Azeez
Nov 11 at 20:02
There's no "right" answer to this question, as there is no way to do what you wish to do. Hence you need to pick a work-around that is the most appropriate, or the least annoying, in your circumstances.
– dasblinkenlight
Nov 11 at 20:09
@dasblinkenlight Sorry but...which answer there is the right one? can't really tell
– Omar Abdul'Azeez
Nov 11 at 20:02
@dasblinkenlight Sorry but...which answer there is the right one? can't really tell
– Omar Abdul'Azeez
Nov 11 at 20:02
There's no "right" answer to this question, as there is no way to do what you wish to do. Hence you need to pick a work-around that is the most appropriate, or the least annoying, in your circumstances.
– dasblinkenlight
Nov 11 at 20:09
There's no "right" answer to this question, as there is no way to do what you wish to do. Hence you need to pick a work-around that is the most appropriate, or the least annoying, in your circumstances.
– dasblinkenlight
Nov 11 at 20:09
add a comment |
1 Answer
1
active
oldest
votes
For primitive types this is not possible as arrays store primitive values themselves not references. You need to create your own class which will incapsulate values you need and then use arrays of elements of your custom class.
For classes arrays store references to the objects so in that case you will be able to achieve what you want.
I thought like... since arrays are considered objects they are a reference to the values inside them so ifshape1XY
is afloat[4]
i can doshape1XY=shape1
and now if i change any of the 4 values inshape1XY
the original array will be changed as well so... why can't i take a part of that reference and not the reference to all 4 values
– Omar Abdul'Azeez
Nov 11 at 19:25
@Omar Because in an object hierarchy references to leaves are different than references to branches. Leaves are immutable, branches generally not. An array would be a branch, its float contents would be leaves
– Perdi Estaquel
Nov 12 at 4:42
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For primitive types this is not possible as arrays store primitive values themselves not references. You need to create your own class which will incapsulate values you need and then use arrays of elements of your custom class.
For classes arrays store references to the objects so in that case you will be able to achieve what you want.
I thought like... since arrays are considered objects they are a reference to the values inside them so ifshape1XY
is afloat[4]
i can doshape1XY=shape1
and now if i change any of the 4 values inshape1XY
the original array will be changed as well so... why can't i take a part of that reference and not the reference to all 4 values
– Omar Abdul'Azeez
Nov 11 at 19:25
@Omar Because in an object hierarchy references to leaves are different than references to branches. Leaves are immutable, branches generally not. An array would be a branch, its float contents would be leaves
– Perdi Estaquel
Nov 12 at 4:42
add a comment |
For primitive types this is not possible as arrays store primitive values themselves not references. You need to create your own class which will incapsulate values you need and then use arrays of elements of your custom class.
For classes arrays store references to the objects so in that case you will be able to achieve what you want.
I thought like... since arrays are considered objects they are a reference to the values inside them so ifshape1XY
is afloat[4]
i can doshape1XY=shape1
and now if i change any of the 4 values inshape1XY
the original array will be changed as well so... why can't i take a part of that reference and not the reference to all 4 values
– Omar Abdul'Azeez
Nov 11 at 19:25
@Omar Because in an object hierarchy references to leaves are different than references to branches. Leaves are immutable, branches generally not. An array would be a branch, its float contents would be leaves
– Perdi Estaquel
Nov 12 at 4:42
add a comment |
For primitive types this is not possible as arrays store primitive values themselves not references. You need to create your own class which will incapsulate values you need and then use arrays of elements of your custom class.
For classes arrays store references to the objects so in that case you will be able to achieve what you want.
For primitive types this is not possible as arrays store primitive values themselves not references. You need to create your own class which will incapsulate values you need and then use arrays of elements of your custom class.
For classes arrays store references to the objects so in that case you will be able to achieve what you want.
answered Nov 11 at 19:16
Ivan
4,6441720
4,6441720
I thought like... since arrays are considered objects they are a reference to the values inside them so ifshape1XY
is afloat[4]
i can doshape1XY=shape1
and now if i change any of the 4 values inshape1XY
the original array will be changed as well so... why can't i take a part of that reference and not the reference to all 4 values
– Omar Abdul'Azeez
Nov 11 at 19:25
@Omar Because in an object hierarchy references to leaves are different than references to branches. Leaves are immutable, branches generally not. An array would be a branch, its float contents would be leaves
– Perdi Estaquel
Nov 12 at 4:42
add a comment |
I thought like... since arrays are considered objects they are a reference to the values inside them so ifshape1XY
is afloat[4]
i can doshape1XY=shape1
and now if i change any of the 4 values inshape1XY
the original array will be changed as well so... why can't i take a part of that reference and not the reference to all 4 values
– Omar Abdul'Azeez
Nov 11 at 19:25
@Omar Because in an object hierarchy references to leaves are different than references to branches. Leaves are immutable, branches generally not. An array would be a branch, its float contents would be leaves
– Perdi Estaquel
Nov 12 at 4:42
I thought like... since arrays are considered objects they are a reference to the values inside them so if
shape1XY
is a float[4]
i can do shape1XY=shape1
and now if i change any of the 4 values in shape1XY
the original array will be changed as well so... why can't i take a part of that reference and not the reference to all 4 values– Omar Abdul'Azeez
Nov 11 at 19:25
I thought like... since arrays are considered objects they are a reference to the values inside them so if
shape1XY
is a float[4]
i can do shape1XY=shape1
and now if i change any of the 4 values in shape1XY
the original array will be changed as well so... why can't i take a part of that reference and not the reference to all 4 values– Omar Abdul'Azeez
Nov 11 at 19:25
@Omar Because in an object hierarchy references to leaves are different than references to branches. Leaves are immutable, branches generally not. An array would be a branch, its float contents would be leaves
– Perdi Estaquel
Nov 12 at 4:42
@Omar Because in an object hierarchy references to leaves are different than references to branches. Leaves are immutable, branches generally not. An array would be a branch, its float contents would be leaves
– Perdi Estaquel
Nov 12 at 4:42
add a comment |
@dasblinkenlight Sorry but...which answer there is the right one? can't really tell
– Omar Abdul'Azeez
Nov 11 at 20:02
There's no "right" answer to this question, as there is no way to do what you wish to do. Hence you need to pick a work-around that is the most appropriate, or the least annoying, in your circumstances.
– dasblinkenlight
Nov 11 at 20:09