Skip to main content

How to print an array in Java

print an array in Java

An array is a type of variable that can store multiple values with an index. This allows developers to modify, arrange, and organize large data sets. Something that developers need to do often, is print an array in Java. In this post, we’ll explore how to do that.

See also: How to use arrays in Python

How to print an array in Java – the easy way

There are a few different types of array in Java and a few ways to print each of them.

The main Java Array looks like this:

String vegetables[] = {"broccoli", "cauliflower", "potato", "carrot", "spinach", "beans"};

This is an array that contains the names of different vegetables. I can print any element from that list like so:

System.out.println(vegetables[3]);

In order to print an array in its entirety, I would simply need to create a little loop.

int i = 0;
while (i < vegetables.length) {
    System.out.println(vegetables[i]);
    i++;
}

That, very simply, is how to print an array in Java.

How to print other types of array

An Array List is an array that can change size at runtime. This means you can add and remove new elements.

The great news is that this type of array can be printed in its entirety with no need for a loop. It’s even easier:

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {

    ArrayList<String> arrayListOfFruit = new ArrayList<String>();
    arrayListOfFruit.add("Apple");
    arrayListOfFruit.add("Orange");
    arrayListOfFruit.add("Mango");
    arrayListOfFruit.add("Banana");
    System.out.println(arrayListOfFruit);

  }

}

Of course, the same loop trick will work just as well!

A map is a type of array in Java that allows you to assign unique key/value pairs that stay the same. This way, you can create something like an address book, where each number (value) is given a contact name (key).

You can print an entire map, just like you can print an Array List:

import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
  
    Map<String, String> phoneBook = new HashMap<String, String>();
    phoneBook.put("Adam", "229901239");
    phoneBook.put("Fred", "981231999");
    phoneBook.put("Dave", "123879122");
    System.out.println(phoneBook);
    

  }
}

However, you also have the option to print individual elements from the map:

System.out.println("Adam's Number: " + phoneBook.get("Adam"));

Closing comments

So, now you know how to print an array in Java!

If you want to learn more tricks of the trade, then be sure to check out our Java tutorial for beginners. Alternatively, why not get a more comprehensive education from one of our best resources to learn Java.



source https://www.androidauthority.com/print-an-array-in-java-1151338/

Comments

Popular posts from this blog

LIVE Day Trading Morning Show for Tuesday!

LIVE Day Trading Morning Show for Tuesday! via YouTube https://www.youtube.com/watch?v=vgagRBXRJM0 Android Tips and Tricks working hard to get the latest and useful tips and techniques that help you to solve a problem or improve your productivity.

5 tips to Voice Speech Recognition in Android Marshmallow

Android Marshmallow landed on our Android devices. The opportunity for us to offer a small compilation of tricks to try immediately. The latest version of the Google OS starts (very gently, unhurriedly) to be offered on other devices as Nexus. You will find on Android TM, in the coming weeks, a compilation of the best tips for Android 6.0 Marshmallow. It starts slowly with a video featuring the 10 essential tips & tricks of this new version of the little green robot OS. To continue, we offer a selection of five "tricks" on the management of the battery on Android 6.0 Marshmallow. To enjoy longer your newly updated mobile. Follow the guide. then continue with 5 tips to tame the super-assistant Google Now on Tap. Here you will find 5 "tips" to manage in the best way your applications. We then discuss the quick tips to navigate more easily on this version of the Google OS. We enchanters with features focused on safety and the protection of personal data. We co...

Android Studio 4.1 – New features for devs

Android Studio 4.1 made it onto the stable channel last month, bringing a number of useful new features for Android developers. There are some interesting inclusions this time around, so read on to see what you need to know! Embedded emulator One of the most interesting updates comes to the Android emulator, which is now built right into the IDE itself. In other words, the emulator no longer opens as a separate app but rather in a tool window. This is said to “save screen real-estate” for all us peasants working on sub 4K, 42” monitors. See also:  Android Studio tutorial for beginners While this is mostly a cosmetic change, it does make the experience feel more cohesive and intuitive. It’s also easier to monitor code while the app is running. It is a great stride toward a more integrated and seamless experience. The feature is not turned on by default, however. To access it, you first need to update your preferences by going to File > Settings > Tools > Emulator ...