Menu Close

Articles

Java: Arrays

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

Example:

 

The above program takes two different arrays and smushes them into one array. Why not just tell the compiler to concatenate the two arrays like we would a String?

 

Because once an array’s size is defined, that’s it. You can’t shrink it or make it bigger. Think of it like having a box which can hold, lets say, 4 items. Its not like you can merge the box with another box, because both boxes have already been created.

 

What we can do is take both boxes, measure them, and create a new box. The box will be able to fit all the elements from each box into one.