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

4 Surprising Ways Artificial Intelligence will Empower Consumers

Ever wonder what makes Siri search the items inside the iPhone and on the web by recognizing your voice? How does Google listening work? Artificial Intelligence and data science have already infused in this consumer-oriented generation, but we have not realized it yet. It has been quite a while that natural language processing, speech recognition, and gesture recognition system took over the market with their all new features, but what are the new things in AI that are yet to come? Few amazing transformations are: Retailing sector to become AI-ready Voice Recognition will change the game Search engines to become smarter Machine learning to bring revolutionary changes Recently we got to learn about a verbal spat between Elon Musk and Mark Zuckerberg over the probable risk and opportunities brought by AI . Believe it or not, but we already accustomed to Artificial Intelligence and probably waiting to witness jaw-dropping inventions. Some of AI inventions we have known till yet are: Mach

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.

How to use arrays in Python

Arrays in Python give you a huge amount of flexibility for storing, organizing, and accessing data. This is crucial, not least because of Python’s popularity for use in data science. But what precisely is an array? And how do you use arrays in Python? Also read: How to use dictionaries in Python Read on, and we’ll shed some light on the matter. What is an array? An array is a way to store multiple values in a single variable. That means that you can use a single “reference” in order to access your data. A list is also an example of a variable that stores multiple values, but has some slight differences. When using lists in Python, you store a series of values each with a numbered index. For example, this is how you would create a list of fruits in Python: fruits = [“apple”, “orange”, “pear”, “nectarine”] If we then say: print(fruits[3]) We will see “nectarine” appear on the screen (the first entry is stored as “0”). Also read: How to use lists in Python This is not an