×

Java Arrays

An array is an object that stores a collection of values. An array can store two types of collection data: objects and primitives.

An array is a collection of values which are heap memory addresses.

In the case of primitive, it stores direct value, and in case of an object, it stores references to the object.

An array stores its object in a continuous fashion which results in improved access speed.

Example:

int[] ArrayNum = new int[] { 1,2,0 };
String[] ArrayObj = new String[] { "Ram", "Mohan", "Roy","INDIA" };
   

Array Initialization

int array[] // deceleration
array[] = new int [2]; //allocation
for (int i=0; i<array .length; i++) {    // initialization using loop
array[i] = i + 5;
}

Some valid array:

1.  String array[] = {"AA","BB","ss"};

2.  int[] a = new int []{1,2,3,4,5};

3.  String array2[] = new String[5];

array2[1]= "ZZ";

array2[2]= "SS";

array2[3]= "FF";

4. int intArray2[] = new int[]{0, 1};

5. String[] strArray2 = new String[]{"Summer", "Winter"};

6. int multiArray2[][] = new int[][]{ {0, 1}, {3, 4, 5}};

Another important point to note is that if we declare and initialize an array using two separate lines of code, we’ll use the keyword new to initialize the values. The following lines of code are correct:

int intArray[];
intArray = new int[]{0, 1};

But you can’t miss the keyword new and initialize your array as follows:

int intArray[];
intArray = {0, 1};

Passing Arrays to Methods

As we can pass primitive type values to methods, we can also pass arrays to methods.

For example, the following method displays the elements in an int array.

Example:

public class MyClass {
public static void main(String[] args) {
printArray(new int[]{1,2,3,4,5,6});
}
public static void printArray(int[] array) {
  for (int i = 0; i < array.length; i++) {
      System.out.print(array[i] + " ");
  }
}
}

Output:

1 2 3 4 5 6

printArray(new int[]{1,2,3,4,5,6}); statement invokes the printArray method to display 1,2,3,4,5,6.

Returning an Array from a Method

A method may also return an array. For example, the following method returns an array that is the reversal of another array:

Example:

public class MyClass {
public static void main(String[] args) {
int array[] = {1,2,3,4,5,6};
for(int x:reverse(array)) {
System.out.print(x+", " );
}
}
public static int[] reverse(int[] list) {
  int[] result = new int[list.length];
  for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
      result[j] = list[i];
  }
  return result;
}
}

Output:

6, 5, 4, 3, 2, 1,

Related Topics

Flag Pattern in Java

The flag pattern in Java can be printed, which will be covered in this part. Given how difficult they are to code, flag patterns are rarely asked by interviewers. We separate...

2 minutes read.

Java Math.multiplyExact() method in Java

Java has an inbuilt math function called Math.multiplyExact() that returns the sum of the parameters. If the result exceeds an integer, an exception is thrown. There is no need to...

2 minutes read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.

Java Integer lowestOneBit()

The lowestOneBit () method of Java Integer class returns an int value with at most a single one-bit, in the position of the lowest-order one-bit in the specified int value.  Syntax public...

2 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity. Syntax: public static int round(float a)public...

2 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

3 minutes read.

JIT in Java

What is JIT ? JIT stands for " Just in Time Compiler ". It is called "Just in time" because it is called at the very last moment when the interpretation...

5 minutes read.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and...

3 minutes read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

Java Integer rotateRight() method

The rotateRight() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value right by the specified number of bits. Syntax public...

1 minute read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

Java Rename File

Renaming a file is the process of changing its name. Using the renameTo() function of the Java File class, renaming operations are possible. A file can be renamed using Java's renameTo()...

3 minutes read.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

XNOR operator in Java

The opposite of the binary equivalent of XOR is given by XNOR. Truth table: XYXNOR001010100111 If the bits are the same, it returns 1, else it returns 0. Examples:  Input: 10 20 Output : 1 A Binary...

3 minutes read.

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It...

3 minutes read.