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.

How to unhide or show folders in mx player list

In this blog post, I tell you about how to Show or Hide folders from MX player list. There are two methods to Show folder from MX Player list. Method 1: Unhide / show folders If you want to temporarily Show / Unhide hidden folder from MX Player list, then go to Settings and untick Recognize .nomedia ".  Method 2: Permanently unhide / show folder: Open memory by any file explorer and I recommend X-Plore, and open the folder that is Hidden and find the file " .nomedia ". If you didn't find it, you should first enable "Show files hidden files that starts with .(dot)". Delete the file and you just need to refresh MX Player list to take changes. Note: MX Player always hide those folders which file " .nomedia " exists.

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...