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

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

Energy Android TV Play turns your TV into a Smart TV

ENERGY SISTEM Android Energy TV Play, you have a smart TV with Android operating system allows you to convert any traditional TV has announced the launch of a new product. Energy Android TV Play can be connected to the TV to enjoy f the size of a flash drive, a smart phone, a tablet and a computer unconsolidated is a lightweight device. System 1.6 GHz, DDR3 1GB of RAM and a dual-core processor can be expanded using external USB devices, which is the internal memory of 8 GB. It also integrates WiFi and a USB port for connecting external devices. One of its outstanding features, it is easily connected to the TV screen by screen cast application to display the contents of any terminal, making any phone or tablet is synchronized with iOS or Android. All ENERGY SISTEM products one click In addition, through streaming media service applications, images, video or other multimedia content, and game play is the ability to share. With integrated WiFi, the device you want from t...

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.