kids encyclopedia robot

Array data structure facts for kids

Kids Encyclopedia Facts

An array is a special way to store a list of things in programming languages. Think of it like a row of lockers, where each locker can hold one item. All the items in an array must be of the same type. For example, you can have an array of only numbers, or an array of only words (called strings), but not a mix of both.

Each item in an array has its own special number, called an index. This index helps a programmer find and use a specific item. In some programming languages, the first item is at index 0, the second at index 1, and so on. Other languages start counting from index 1.

When you create an array, you usually have to decide its size right away. This means you tell the computer how many items the array can hold. Once an array is created, its size usually cannot be changed. If you need to store more items than the array can hold, you'll need to create a brand new, bigger array.

How Do Arrays Work?

Arrays are like organized containers for data. They help programmers keep related information together. Imagine you want to store the scores of five players in a game. Instead of making five separate variables (like `score1`, `score2`, etc.), you can use one array called `scores`.

Storing Information in an Array

To put information into an array, you use its index. For example, if you want to put the number 10 into the first spot of an array named `myNumbers`, you might write something like `myNumbers[0] = 10;` (if it starts counting from 0).

Getting Information from an Array

You can also get information out of an array using its index. If you want to see what's in the third spot of `myNumbers`, you might write `print(myNumbers[2]);`. This would show you the value stored at that specific index.

Arrays in C Programming

The C programming language is one of the oldest and most powerful languages. In C, you create an array by first saying what type of data it will hold, then giving it a name, and finally putting its size in square brackets.

For example, to make an array that can hold 5 integers (whole numbers), you would write:

int array[5];

This line tells the computer to set aside space for 5 integers.

Now, to put numbers into this array, you use the index for each spot. Remember, C starts counting from index 0.

array[0] = 1;   // The first spot gets the number 1
array[1] = 18;  // The second spot gets 18
array[2] = 5;   // The third spot gets 5
array[3] = 33;  // The fourth spot gets 33
array[4] = 50;  // The fifth spot gets 50

You can also use the numbers stored in the array in your calculations.

int k = 3 + array[3];  // This means k will be 3 + 33, so k becomes 36

Arrays in Java Programming

Java is another very popular programming language, often used for apps and websites. Creating arrays in Java is a bit similar to C, but with a slightly different way of writing it.

To make an array that can hold 5 integers in Java, you would write:

int[] array = new int[5];

This line also creates space for 5 integers.

Just like in C, Java arrays also start counting from index 0. So, to put numbers into this array:

array[0] = 1;   // The first spot gets the number 1
array[1] = 18;  // The second spot gets 18
array[2] = 5;   // The third spot gets 5
array[3] = 33;  // The fourth spot gets 33
array[4] = 50;  // The fifth spot gets 50

And you can use the values from the array in Java too:

int k = 3 + array[3];  // Here, k will also become 3 + 33, which is 36

See also

Kids robot.svg In Spanish: Vector (informática) para niños

kids search engine
Array data structure Facts for Kids. Kiddle Encyclopedia.