Week 60 — What is an array and how can one access its elements?
Question of the Week #60
What is an array and how can one access its elements?
10 Replies
Java allows creating variables storing data.
However, in some cases, it's necessary to store the same type of data multiple times. This is possible using arrays.
Arrays are a block of memory where a given amount of data is contained. When creating an array, one needs to specify the element type of the array and the length. After creating the array, it can store the element as often as specified by the length.
Each entry in an array is identified by an (integer) index starting with 0 and going up (not including) the length.
Arrays are declared by specifying the element type followed by
[]
and can be created using the new keyword followed by []
with the length inside the square brackets:
Elements in that array can be set like a variable but one needs to specify the index in square brackets:
It is also possible to look up elements of the array in a similar way:
Furthermore, it is possible to loop over all the elements of the array:
The method Arrays.toString
can be used to print the content of the array:
📖 Sample answer from dan1st
An array is a data structure that can contains data from the same type.
Each value has an index, starting at 0.
To access an element, one need to use [] behind the array name, using the index of the element to get :
Submission from maruvert
Is a collection of objects of same data type.
An element can be accessed by writing the name of the array beside it square parquets having the index of the element we want to access, like when printing the element.
Like this:
int[] array = {5,1,2,3,4};
System.out.println(array[0]);
Submission from herofoty
An array is a list of elements that are indexed from 0 to a the 3 for example if there were four elements in the array. Elements can be called via Array[0]
Submission from theoneandonlylark
collection of elements that are of the same type. using square brackets for example arrayName[0]
Submission from abc8671
An Array is a list of a certain data type and a person can access its contents by using
arrayName[index]
when index is an integer representing an index of the array.Submission from thespiky.hedgehog
An Array is a reference type that points to an object where the array is located. It contains zero to multiple values, you can access these values with the syntax arrayName[x] where ‘x’ is an index to a specific value on the array in order starting at 0.
Example:
String[] myArray = new String[2];// Creating that will contain two string values.
//adding values
myArray[0]= “Hello”;
myArray[1]=“Group”;
To access those elements:
System.out.print(myArray[0]); // prints Hello
Submission from navatante
An array in Java is a data structure that holds a fixed number of elements of the same data type. To declare an array in Java, you specify the data type of the elements followed by square brackets
[]
and then the array name. Here's how you declare an array of integers:
You can also initialize the array with specific values:
To access elements of an array in Java, you use square brackets []
notation, also known as the array subscript operator, with the index of the element you want to access. The index starts from 0 for the first element and goes up to length - 1
, where length
is the total number of elements in the array.
Here's an example demonstrating how to access elements of an array in Java:
In this example, numbers[0]
accesses the first element of the array numbers
The output of the program will be:
⭐ Submission from ig.imanish