0

I just saw this code on the web and can't think of what array of 'b' would be.

int[][] a = {{4,2},{3,6,8}};
int[] b = a[1]
b[0] = 5;  
2
  • a[0] = {4,2} and a[1] = {3,6,8}. You are assigning a[1] to b. So b would be {3,6,8}. Commented Nov 15, 2016 at 22:37
  • What do you mean, you can't think of what array of 'b' would be? You don't know what is the type of b? Commented Nov 15, 2016 at 22:38

5 Answers 5

3

Array a is made up of multiple arrays

a[0] = {4,2};
a[1] = {3,6,8};

Array b is made up of the second value within array a,

int[][] a = {{4,2},{3,6,8}};
int[]   b = {3,6,8};

b[0] would be the first element within array b, which in this case replaces 3.

b[0] = 5; //b = {5,6,8}
Sign up to request clarification or add additional context in comments.

Comments

1

The int array a is a 2D array which consists of two 1-D arrays: [4,2] and [3,6,8].

We have then initialised a 1D array referred to as 'b' which has been set to the second (1-index) array of a.

Therefore b = [3,6,8]. 'b' refers to the first 1D array in 'a'.

You finally set the 0th index of b's array to be 5. b[0]=5

This means b has now become b=[5,6,8]

Comments

1

Well, a two dimensional array has both a row and a column

In this case, a[1] is asking for the second row of a. Remember that index begins at 0.

So, the second row of a would be {3,6,8}.

Therefore b = a[1] evaluates to b = {3,6,8}

Then, b[0] is accessing the first index of b, which is 3 and changing it to 5. So the end result is {5,6,8}.

Comments

1

Think of a 2 dimensional array as an array of arrays.

a[0] is [4,2]

a[1] is [3,6,8]

So if you assign a[1] to b, b becomes [3,6,8]. Once you assign 5 to b[0], b becomes [5,6,8]

Comments

0

a is a two dimension array, Inorder to access a case in that array you need two index variables,..think of it as matrix... There is line and column to store the variables or you can think of ot like array of arrays.. So at first b = {3,6,8} because a[1] is the second array, then you modified the forst element in b, so b ={5,6,8}

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.