Java int array type

Java Array of Integers

Java Integer Array is a Java Array that contains integers as its elements. Elements of no other datatype are allowed in this array.

In this tutorial, we will learn how to declare a Java Int Array, how to initialize a Java Int Array, how to access elements of it, etc.

How to declare an Integer Array in Java?

Following is the syntax to declare an Array of Integers in Java.

You can use any of these two notations.

How to initialize an Integer Array?

To initialize an integer array, you can assign the array variable with new integer array of specific size as shown below.

You have to mention the size of array during initialization. This will create an int array in memory, with all elements initialized to their corresponding static default value.

The default value for an int is 0 .

Following is an example program to initialize an int array of size 10.

Java Program

We have declared and initialized the int array in two different statements. But you can combine the declaration and initialization, to form the definition of int array, as shown below.

Java Program

In the above example, we have created a string array named numbers, and initialized it to an int array of size 10 with default values of 0.

You can also assign int values directly to the integer array when declaring it.

In the following example, we have declared and initialized int array with elements.

Java Program

Now numbers is an integer array with size of 7, because there are seven elements in the array we assigned.

How to access Integer Array Elements?

Following is the syntax to access an element of an array using index.

The above statement can be used either to read an element at given index, or set an element at given index. The read or write operation depends on which side of assignment operator you are placing this.

For example, in the following program, we are reading the element of int array at index 2.

Java Program

public class IntArray < public static void main(String[] args) < int numbers[] = ; int number = numbers[2]; System.out.println(number); > >

And in the following example, we are updating the element of int array at index 2 to 85.

Java Program

public class IntArray < public static void main(String[] args) < int numbers[] = ; numbers[2] = 85; System.out.println(numbers[2]); > >

How to iterate over array elements?

We can use any of these looping statements like For Loop or While Loop to iterate over elements of a Java Array.

In the following example, iterate over elements of Integer Array using Java While Loop.

Java Program

public class IntArray < public static void main(String[] args) < int numbers[] = ; int index = 0; while (index < numbers.length) < System.out.println(numbers[index]); index++; >> >

In the following example, we will iterate over elements of Integer Array using Java For Loop.

Java Program

public class IntArray < public static void main(String[] args) < int numbers[] = ; for (int index = 0; index < numbers.length; index++) < System.out.println(numbers[index]); >> >

And in the following example, we will use Java for-each loop, to iterate over elements of integer array. For each loop can be used to execute a set of statements for each string in integer array.

Java Program

public class IntArray < public static void main(String[] args) < int numbers[] = ; for (int number: numbers) < System.out.println(number); >> >

Conclusion

In this Java Tutorial, we learned about Integer Array in specific: how to declare an int array, how to initialize it, how to access elements, etc.

Источник

Arrays in Java

Array in java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. Following are some important points about Java arrays.

  • In Java, all arrays are dynamically allocated. (discussed below)
  • Arrays are stored in contiguous memory [consecutive memory locations].
  • Since arrays are objects in Java, we can find their length using the object property length. This is different from C/C++, where we find length using sizeof.
  • A Java array variable can also be declared like other variables with [] after the data type.
  • The variables in the array are ordered, and each has an index beginning with 0.
  • Java array can also be used as a static field, a local variable, or a method parameter.
  • The size of an array must be specified by int or short value and not long.
  • The direct superclass of an array type is Object.
  • Every array type implements the interfaces Cloneable and java.io.Serializable.
  • This storage of arrays helps us randomly access the elements of an array [Support Random Access].
  • The size of the array cannot be altered(once initialized). However, an array reference can be made to point to another array.

An array can contain primitives (int, char, etc.) and object (or non-primitive) references of a class depending on the definition of the array. In the case of primitive data types, the actual values are stored in contiguous memory locations. In the case of class objects, the actual objects are stored in a heap segment.

Creating, initializing, and accessing an Array

One-Dimensional Arrays:

The general form of a one-dimensional array declaration is

type var-name[]; OR type[] var-name;

An array declaration has two components: the type and the name. type declares the element type of the array. The element type determines the data type of each element that comprises the array. Like an array of integers, we can also create an array of other primitive data types like char, float, double, etc., or user-defined data types (objects of a class). Thus, the element type for the array determines what type of data the array will hold.

// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass (a class created by // user) MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type

Although the first declaration establishes that int Array is an array variable, no actual array exists. It merely tells the compiler that this variable (int Array) will hold an array of the integer type. To link int Array with an actual, physical array of integers, you must allocate one using new and assign it to int Array.

Instantiating an Array in Java

When an array is declared, only a reference of an array is created. To create or give memory to the array, you create an array like this: The general form of new as it applies to one-dimensional arrays appears as follows:

Here, type specifies the type of data being allocated, size determines the number of elements in the array, and var-name is the name of the array variable that is linked to the array. To use new to allocate an array, you must specify the type and number of elements to allocate.

int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array
int[] intArray = new int[20]; // combining both statements in one

Note:

The elements in the array allocated by new will automatically be initialized to zero (for numeric types), false (for boolean), or null (for reference types). Do refer to default array values in Java.

Obtaining an array is a two-step process. First, you must declare a variable of the desired array type. Second, you must allocate the memory to hold the array, using new, and assign it to the array variable. Thus, in Java, all arrays are dynamically allocated.

Array Literal

In a situation where the size of the array and variables of the array are already known, array literals can be used.

int[] intArray = new int[]< 1,2,3,4,5,6,7,8,9,10 >; // Declaring array literal
  • The length of this array determines the length of the created array.
  • There is no need to write the new int[] part in the latest versions of Java.

Accessing Java Array Elements using for Loop

Each element in the array is accessed via its index. The index begins with 0 and ends at (total array size)-1. All the elements of array can be accessed using Java for Loop.

// accessing the elements of the specified array for (int i = 0; i < arr.length; i++) System.out.println("Element at index " + i + " : "+ arr[i]);

Implementation:

Источник

Читайте также:  All string methods in python
Оцените статью