All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Initialize Array In Java

Last Updated : Jul 1, 2023

How To Initialize Array In Java

In this article we will show you the solution of how to initialize array in java, initialising an array in Java refers to the procedure of generating an object for an array and giving each of its component’s initial values.

In Java, arrays are objects that keep several items of the same type together in a single block of memory.

Array initialization can take place in a number of different ways, including using the keyword new to create a new array object & specify its size, with array literals to immediately assign value to array elements at declaration, and using loop to assign values to elements of an array in a variable-time manner.

The right access and manipulation of array elements in Java programmes depend on proper array initialization. Let's talk about the idea of initialising an array in Java now.

Step By Step Guide On How To Initialize Array In Java :-

import java.util.Random;
public class Arrays {
    public static void main(String[] args) {
        int arrayTwo[] = new int[3];
        int[] arrayOne = new int[3];
        String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
        System.out.println("suits[3]=" + suits[3]);
        System.out.println();
        String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
        System.out.println("suits.length=" + suits.length);
        System.out.println();
        int x = new Random().nextInt(ranks.length);
        int y = new Random().nextInt(suits.length);
        System.out.println(ranks[x] + " of " + suits[y] + "\n");
        System.out.println();
        String[] deck = new String[ranks.length * suits.length]; // vs [52]
        for (int i = 0; i < ranks.length; i++) {
            for (int j = 0; j < suits.length; j++) {
                deck[suits.length * i + j] = ranks[i] + " of " + suits[j];
            }
        }
        int[][] twoD = new int[][] {
          {1, 2, 3, 4},
          {5, 6, 7, 8}
        };
        System.out.println("twoD[1][2]=" + twoD[1][2]);
        System.out.println(deck);
        System.out.println();
        for (int j = 0; j <= suits.length; j++) {
            System.out.println("suits[j]=" + suits[j]);
        }
        int[] array = makeArray();
        for (int i : array) {
            System.out.print(i + " ");
        }
    }
    public static int[] makeArray() {
        int[] myArray = new int[10]; // new == heap
        for (int i = 0; i < myArray.length; i++) {
            myArray[i] = i;
        }
        return myArray;
    }
}
  1. As you can see, we've created this initialization code for the array in Java.
  2. We import the Random class within the java.util package at the beginning of the code, which enables for the creation of random integers.
  3. Next, we create a class called Arrays that has a main function that acts as the program's launchpad.
  4. Following that, we declare and set up two 3-length integer arrays with the names arrayTwo and arrayOne.
  5. Then we make a string array called suits that contains the words "Clubs," "Diamonds," "Hearts," and "Spades," respectively.
  6. Use System.out.println() to then print the value located at index 3 of the suits array.
  7. Then, we make a string array called ranks, which has thirteen entries that represent the ranks of playing cards.
  8. Additionally, use System.out.println() to display the suits array's length.
  9. Then use Random to produce the two random numbers x & y.When using the nextInt() method, x and y must be between 0 and the length of the ranks array (exclusive) & the length of the suits array, respectively.
  10. Then, display a playing card chosen at random using the elements of the ranks and suites arrays accessed by the randomly created indices x and y.
  11. Then make a string array deck, that represents a deck of playing cards, with a length equivalent to the product of the lengths of the ranks and suites arrays.
  12. After that, we utilise nested loops to add combinations of ranks & suits to the deck array using those combinations' indices.
  13. Then we build a two-dimensional integer array twoD with two rows and four columns and initialise it with values.
  14. In the final line, we use System.out.println() to print the value located at index [1][2] of the twoD array.

Conclusion :-

As a result, we were able to effectively understand the Java notion of array initialization.

Additionally, we studied the fundamentals of indexing, looping, and sending arrays to Java methods.

It gives a concrete illustration of how to construct, work with, and gain access to arrays of various data kinds, such as strings and integers.

It also demonstrates how to create random values using the Random class and how multi-dimensional arrays work.

I hope this article on how to initialize array in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪