#Week 60 — What is an array and how can one access its elements?

11 messages · Page 1 of 1 (latest)

heavy thicketBOT
#
Question of the Week #60

What is an array and how can one access its elements?

cyan emberBOT
#

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:

int[] someArray = new int[10];//create an array storing 10 ints

Elements in that array can be set like a variable but one needs to specify the index in square brackets:

someArray[0] = 123;//set the first element (index 0) of the array to 123

It is also possible to look up elements of the array in a similar way:

System.out.println(someArray[0]);//123

Furthermore, it is possible to loop over all the elements of the array:

for(int i=0; i<someArray.length; i++){
    someArray[i] = i+1;//sets each element to its index + 1
}
System.out.println(someArray[5]+1);//6

The method Arrays.toString can be used to print the content of the array:

System.out.println(Arrays.toString(someArray));//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10
📖 Sample answer from dan1st
cyan emberBOT
#

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 :

int foo = bar[2]; //foo is now equal to the 3th element of the bar array
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