Skip to document

An Overview of Arrays

An Overview of Arrays
Course

Computer science

702 Documents
Students shared 702 documents in this course
Academic year: 2020/2021
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
University of Southern Mindanao

Comments

Please sign in or register to post comments.

Preview text

An Overview of Arrays

How to declare various variables using various data types was covered in earlier chapters. A distinct identifier name and data type should be used when declaring variables. The variable's identifier name is called in order to use it.

For instance, the following three (3) int variables each have a unique name: int num1 = 0; int num2 = 0; int num3 = 0;

num1 = 1; num2 = 3; num3 = 5;

The initialization, assignment, and use of the variables appear to be laborious tasks. One feature of Java and other programming languages allows for the efficient storage and manipulation of a list of data in a single variable. An array is the name for this kind of variable. A continuous block of memory that has been partitioned into a number of slots is used by an array to store many instances of the same type of data. Consider an array as a stretched variable—a location with only one name for identification but the capacity to store multiple values. Instead of defining three (3) variables that can contain one (1) value each, as in the example above, you can define an int array variable named num that can hold three (3) values: 1, 3, and 5 (Fig. 51).

num:

0 1 2

1 3 5

Fig. 51. Visual Representation of an Array Variable

DECLARING ARRAYS

Like other variables, arrays must be declared. List the data type, a series of square brackets [], the name of the array's identifier, and then the data type. For instance,

int []num;

The square brackets can also be placed after the identifier name. For example,

int num[];

The array must be constructed after declaring it, and a constructor statement must provide its length. In Java, this procedure is known as instantiation (the Java word for create). Keep in mind that after the array is initialized, the size cannot be modified. For instance,

int num[]; num = new int[100]; or int []num = new int[100];

In the above example, the declaration instructs the Java compiler to construct a new array with 100 members and to use the identifier num as the name of an array that contains integers.

num: 0 1 2 3 4 99 ...

Instead of using the new keyword to instantiate an array, it can also be automatically declared, created and assigned values at once with the elements enclosed in a pair of curly braces ( { } ) and separated by commas ( , ). For example,

boolean results[] = {true, false, true, false};

double []grades = {100, 90, 80, 75};

String days[] = {“Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”,”Sun”};

ACCESSING AN ARRAY ELEMENT

A number known as an index or a subscript is used to access an array element or a section of the array. Each element of the array is given an index or a subscript, enabling the program and the programmer to retrieve specific values as needed. Integers are always used as index numbers. They start at zero (0) and move in whole number order from there until the end of the array. Keep in mind that an array's elements are indexed from 0 to (size of the array – 1). For instance, assuming the array that was initially declared,

num[0] = 10; //assigns 10 as the first element of the array System.out(num[50]); //outputs the 51st element of the array

Be aware that after an array is declared and formed, each member's stored value for numeric data will be set to 0. Reference data types, like Strings, are not initially initialized with blanks or an empty string, though. String arrays must be clearly filled in as a result. The sample code for assigning user inputs to an array's elements is shown below. The code is shorter because a for loop is used.

import javax.swing; public class ArraySample_1{ public static void main(String []args) {

2

MULTIDIMENSIONAL ARRAYS

Arrays of arrays are used to implement multidimensional arrays. Multiple bracket pairs are used to declare multidimensional arrays before or after the array name. For instance,

int [][]twoD = new int [512][128];

char threeD[][][] = new char[8][16][24];

String [][]dogs = {{“Terry”,”Brown”}, {“Rave”,”White”}, {“Toby”,”Gray”,}, {“Fido”,”Black”}};

It is identical to accessing an array element in a one-dimensional array to access an element in a multidimensional array. For instance, if you want to access the first member in the array dogs' first row, you might write,

System.out(dogs[0][0]); //Outputs Terry The following is an example on how to print the elements of a multidimensional array using loops.

public class ArraySample{ public static void main(String []args) { String [][]dogs={{“Terry”,”Brown”},{“Rave”,”White”},{“Toby”,”Gray”}, {“Fido”,”Black”}}; for(int i = 0; i<dogs; i++) { for(int j = 0; j<dogs[i].length; j++){ System.out(dogs[i][j]); } } } }

4

Was this document helpful?

An Overview of Arrays

Course: Computer science

702 Documents
Students shared 702 documents in this course
Was this document helpful?
An Overview of Arrays
How to declare various variables using various data types was covered in
earlier chapters. A distinct identifier name and data type should be used when
declaring variables. The variable's identifier name is called in order to use it.
For instance, the following three (3) int variables each have a unique name:
int num1 = 0;
int num2 = 0;
int num3 = 0;
num1 = 1;
num2 = 3;
num3 = 5;
The initialization, assignment, and use of the variables appear to be
laborious tasks. One feature of Java and other programming languages allows for
the efficient storage and manipulation of a list of data in a single variable. An
array is the name for this kind of variable.
A continuous block of memory that has been partitioned into a number of
slots is used by an array to store many instances of the same type of data. Consider
an array as a stretched variable—a location with only one name for identification but
the capacity to store multiple values. Instead of defining three (3) variables that can
contain one (1) value each, as in the example above, you can define an int array
variable named num that can hold three (3) values: 1, 3, and 5 (Fig. 51).
num: 0 1 2
1 3 5
Fig. 51. Visual Representation of an Array Variable
DECLARING ARRAYS
Like other variables, arrays must be declared. List the data type, a series of
square brackets [], the name of the array's identifier, and then the data type. For
instance,
int []num;
The square brackets can also be placed after the identifier name. For example,
int num[];
The array must be constructed after declaring it, and a constructor
statement must provide its length. In Java, this procedure is known as instantiation
(the Java word for create). Keep in mind that after the array is initialized, the size
cannot be modified. For instance,