Is it possible to copy some of an array's values into another array but with the same reference? [duplicate]












-1















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?










share|improve this question













marked as duplicate by dasblinkenlight arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

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
















-1















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?










share|improve this question













marked as duplicate by dasblinkenlight arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

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














-1












-1








-1








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 19:10









Omar Abdul'Azeez

185




185




marked as duplicate by dasblinkenlight arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

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 arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

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


















  • @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












1 Answer
1






active

oldest

votes


















3














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.






share|improve this answer





















  • 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


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














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.






share|improve this answer





















  • 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
















3














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.






share|improve this answer





















  • 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














3












3








3






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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


















  • 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
















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



Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation