0

I want to fill in part of an array with the values of another array. Is there anyway I can do this without looping?

e.g.

int [] [] ArrayToFillIn = new int [3] [3]
int [] FillingArray = {1, 2};

for (int i = 1; i < 3; i++)
{
    ArrayToFillIn [i-1] [2] = FillingArray [i - 1];
}

in R it would be like:

ArrayToFillIn [c(1:2),3] = FillingArray []

(considering that R does not start from 0)

Thanks!

4
  • Why wouldnt you loop? Commented Feb 17, 2017 at 15:24
  • 5
    Normally you would use Arrays.copyOfRange, but because you're inserting across a 2D array you may not be able to leverage that without loops anyway. Commented Feb 17, 2017 at 15:26
  • Java doesn't have that sort of array splicing functionality. Commented Feb 17, 2017 at 15:40
  • Could you provide an example of the output you want to produce with your example input data? Commented Feb 17, 2017 at 16:02

2 Answers 2

1

Sure. Without a loop it would be

ArrayToFillIn [0] [2] = FillingArray [0];
ArrayToFillIn [1] [2] = FillingArray [1];
Sign up to request clarification or add additional context in comments.

1 Comment

I think The OP is talking about the general case :3
0

What with this initialization:

int [] FillingArray = {1, 2};
int [][] ArrayToFillIn = new int[][]{{0, 0, FillingArray[0]}, {0, 0, FillingArray[1]}, {}};
//---------------------------------------------^-----------------------^----------------^

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.