What an array is and why you need one

An array in Java is a container that holds multiple values of the same type in a single variable. Instead of creating ten separate variables to store ten numbers, you create one array that holds all ten. Arrays are useful because they let you work with groups of related data — like a list of IP addresses on your network, or sensor readings from multiple devices — without writing repetitive code.

Arrays have a fixed size set when you create them. Once you decide an array will hold 5 items, it always holds exactly 5 slots, even if you only fill 3 of them. This is different from some other data structures in Java that grow and shrink, but arrays are faster and simpler when you know ahead of time how many items you need.

Key Takeaways

  • Declare an array by writing the data type, square brackets, and a variable name: int[] numbers; creates a variable that will hold integers.
  • Create the actual array with the new keyword and a size in square brackets: numbers = new int[5]; makes room for 5 integers.
  • You can declare and create an array in one line: int[] numbers = new int[5];
  • Fill array slots using the index number in square brackets, starting from 0: numbers[0] = 10; puts 10 in the first slot.
  • Arrays work with any data type: integers, decimals, text, or objects like network packets or device connections.

The two steps: declaring and creating

Creating an array happens in two separate steps, though you can combine them into one line. First, you declare the array variable by telling Java what type of data it will hold. Write the data type, then square brackets, then the variable name. For example: int[] numbers; tells Java you want a variable called numbers that will eventually hold integers.

Second, you create the actual array using the new keyword. This is where you tell Java how many slots the array needs. Write new, the data type again, and the size in square brackets: numbers = new int[5]; creates an array with 5 slots for integers. You can do both steps at once: int[] numbers = new int[5];

When you create an array, Java fills all the empty slots with default values. For integers, that default is 0. For decimals (type double), it is 0.0. For text (type String), it is null, which means "no value yet".

Putting data into array slots

Each slot in an array has a position number called an index. The first slot is always index 0, the second is index 1, and so on. To put a value into a specific slot, write the array name, the index in square brackets, an equals sign, and the value: numbers[0] = 10; puts the number 10 into the first slot. numbers[4] = 50; puts 50 into the fifth slot.

If you try to use an index that does not exist, Java stops your program and shows an error. If your array has 5 slots (indices 0 through 4) and you try to write to numbers[5], you will get an ArrayIndexOutOfBoundsException. This is Java's way of protecting you from accidentally writing to memory you did not intend to use.

Creating arrays with initial values

Instead of creating an empty array and filling it slot by slot, you can create an array and fill it all at once using curly braces. Write the data type, square brackets, an equals sign, and then list the values in curly braces separated by commas: int[] numbers = {10, 20, 30, 40, 50}; creates an array with 5 slots and fills them when ready. Java counts the values and sets the array size automatically.

This shortcut works with any data type. For text: String[] devices = {"Router", "Switch", "Firewall"}; creates an array of three device names. For decimals: double[] voltages = {3.3, 5.0, 12.0}; creates an array of three voltage readings. You cannot use this shortcut after the array already exists — it only works when you are declaring and creating at the same time.

Reading values back from an array

To get a value out of an array, use the array name and the index in square brackets. int firstNumber = numbers[0]; reads the value from the first slot and stores it in a new variable. You can use array values in calculations: int sum = numbers[0] + numbers[1]; adds the first two values. You can print them: System.out.println(numbers[3]); displays the fourth value.

You can also find out how many slots an array has using the length property. Write the array name, a dot, and the word length: int size = numbers.length; tells you how many slots exist. This is useful when you are looping through an array and need to know when to stop.

Looping through all values in an array

The most common way to work with arrays is to loop through every slot and do something with each value. A for loop is the standard tool. This loop reads every value in the numbers array and prints it:

for (int i = 0; i < numbers.length; i++) {   System.out.println(numbers[i]); }

The loop starts at index 0, checks if it is less than the array length, and adds 1 to the index each time. When the index reaches the length (which is one past the last slot), the loop stops. This pattern works for any array, no matter the size.

Java also has an enhanced for loop that is simpler when you do not need the index number itself. This loop reads every value and stores it in a variable called value:

for (int value : numbers) {   System.out.println(value); }

Both loops do the same thing. Use the first one if you need to know which index you are at. Use the second one if you just want to process each value in order.

Arrays of different data types

Arrays work with any data type Java supports. boolean[] flags = new boolean[3]; creates an array of true/false values. char[] letters = new char[26]; creates an array of single characters. double[] measurements = new double[100]; creates an array of decimal numbers for sensor readings or network latency values.

You can also create arrays of objects. If you have a class called Device that represents a network device, you can create an array of Device objects: Device[] devices = new Device[10]; This creates 10 slots, each of which can hold a Device object. You fill them the same way: devices[0] = new Device("Router");

Frequently Asked Questions

Can I change the size of an array after I create it?

No. Arrays have a fixed size that you set when you create them. If you need to add or remove items later, you have to create a new array of the new size and copy the values over. For situations where the size changes often, Java offers other data structures like ArrayList that grow automatically, but they are slower than arrays.

What happens if I do not fill every slot in an array?

The empty slots keep their default value (0 for numbers, null for objects). Your code can still read from them. It is your responsibility to track which slots actually contain meaningful data and which are just defaults. Many programs use a separate variable to track how many slots are actually in use.

Can an array hold different types of data, like both integers and text?

No. An array holds only one data type. int[] numbers holds only integers. If you need to store mixed types together, you can create an array of Object (the parent type of everything in Java), but then you have to convert each value back to its original type when you read it, which is error-prone. It is usually better to create separate arrays or use a custom class.

How do I copy an array?

straightforward assigning one array to another does not copy it: int[] copy = numbers; makes both variables point to the same array in memory. To create a true copy, use System.arraycopy() or the Arrays.copyOf() method from the java.util package. Both create a new array with the same values.

Can arrays have more than one dimension?

Yes. A two-dimensional array is like a table with rows and columns: int[][] grid = new int[3][4]; creates a 3-by-4 grid. You access it with two indices: grid[0][1] means row 0, column 1. Three-dimensional arrays and higher are possible but less common. Most network and sensor data uses one-dimensional arrays or specialized data structures.