How to create a String or int Array in Java? Example Tutorial

There are several ways to create an array in Java, for example, you can declare the array without initializing it, or you can create and initialize the array in the same line. If you want to make an array with values, then you need to first decide which type of array you want to create? e.g., do you want a String array, which can contain String objects like "abc," "def," or do you want to create an int array that contains int values like 10, 20, etc. In Java, you can create an array of any type, including primitives like byte, int, long, and objects like String, Integer, and other user-defined objects. Let's some code to make a new array in Java.


1. Creating String array in Java

There are three main ways to create a String array in Java, e.g. here is a String array with values :

String[] platforms = {"Nintendo", "Playstation", "Xbox"};

and here is a String array without values :

String[] games = new String[5];

This array can hold 5 String objects because its length is 5. It's mandatory to define the length of a variety while making, and you cannot change the length once created.

You can also do something like this :

String[] actors = new String[5]{"Hero", "Heroine", "Friend", "Villain", "Dragon"};

All there are correct ways to make an array in Java. Though, if you are not familiar with an essential data structure like an array and linked list, then I suggest you first go through these best data structure and algorithms online courses.  It's a very important topic for any programmer be it a core Java developer or Java web developer and you just can not afford to ignore this.

What is an array in Java



How to create an Int array in Java?

You can use the same syntax as shown above to create an int array, all you need to do is replace String with int values as shown below :

making an int array with values

int[] primes = {2, 3, 5, 7};


int array without values :

int[] even = new int[5];

length of this array is 5, hence it can only hold 5 int values. If you don't assign values then by default they hold the default value of their type like int array will have zero in every index, and the String array will have null. You can further see these best Java online courses learn more about different types of the array in Java.

How to make a new array in Java?


How to access array elements  in Java?

You can access array elements using index, e.g. primes[0] will return the first element from an array that is referenced by the prime variable. Remember, the array index starts with zero. If you want to iterate over an array, i.e. wants to go access every element then you can use a loop, e.g. for, while or a do-while loop. 

You can also use advanced for loop in Java, which allows you to iterate over array without index as shown below :

for(String str : platforms){
  System.out.println(str); // Nintendo, Playstation, Xbox
}

The minimum index in the array is zero, and the maximum index is length - 1.


That's all about how to make an array in Java. You have also learned how to initialize the array at the time they are created and how to access them with and without index using advanced for a loop. Though, there are many ways to create an array in Java and even more ways to loop over them. For example, you can also use while, do-while, and classic for loop to iterate over an array in Java.


Other Java array tutorials you may like
Do you want to learn more about the array data structure? The most important Data structure for a programmer - If Yes, here are a couple of more articles which you can explore to learn about Array better:
  • How to create an array from ArrayList of String in Java (tutorial)
  • Top 30 Array-based Coding Problems from Interviews (questions)
  • 7 Best Udemy courses to learn Data Structure and Algorithms (courses)
  • How to remove duplicates from an unsorted array in Java? (solution)
  • 10 Data Structure Courses to Crack Programming Interviews (courses)
  • Best of Data Structure Problems from Interviews (questions)
  • 10 Courses to learn Data Structure and Algorithms in Java (courses)
  • How to find all pairs whose sum is equal to a given number in an array? (solution)
  • 10 Courses to learn Java for Beginners (courses)
  • How to reverse an array in place in Java? (solution)
  • 10 Algorithms Books Every Programmer Should Read (books)
  • 10 Free Data Structure and Algorithm Courses for Beginners (courses)
  • Top 20 Searching and Sorting Interview Questions (questions)
  • How to make a binary search tree in Java? (solution)
  • 10 (Free) Online Courses to Learn Data Structure and Algorithms in Java (courses)
  • 50+ Data Structure and Algorithms Interview Questions (questions)

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are looking to learn Data Structure and Algorithms from scratch or want to fill gaps in your understanding and looking for some free courses, then you can check out this list of Free Algorithms Courses on HackerNoon to start with.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.