We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# Dimensional Array

on
Medium Priority
162 Views
How to create sub array with different length?

[0] - > [0]
[1] -> [0] [1]
[2] -> [0] [1] [2]
[3] -> [0] [1] [2] [3]
Comment
Watch Question

## View Solution Only

Commented:
// initially define only one dimension
int[][] a = new int[4][];

for(int i = 0; i < 4; i++)
{
// when needed, initialize the second dimension with required number of elements
a[i] = new int[i+1];
}

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
After able created my sub array with different length
The initial one dimension array will be all null

if i want to insert a value

Insert value 1
[0] -> [value 1]
[1] -> null
[2] -> null
[3] -> null

Insert value 2
[0] -> [Value 2]
[1] -> [Value 1][]
[2] -> null
[3] -> null

How do i create this insertion of value? bigger value to be store on top

Commented:
void insert(int n) {

// i think this should create the correct size of array
count = count + 1;
a[count] = new int[count+1];

// how to insert the value into the correct slot?

}

Commented:
thanks
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile