dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Jun 21, 2021

Using get(), set(), and size() with Arraylist in Java

In previous articles we learned how to use the add() and remove() methods on ArrayLists to add and remove elements in an array. You can read that article by visiting Using Add() and Remove() ArrayList Methods in Java. If you are curious to learn more about ArrayList in Java, you can also read the first tutorial in this series by reading How to Use Java ArrayList.

Using the set() Method in Java

In this tutorial, we are going to learn how to use the get() and set() methods on ArrayLists. We will start with the set() method, which, in Java, lets us change the value of an element in an array. In the code below, we start by creating an ArrayList called superHero and assign a bunch of string values to it, which, in this case, are used to represent super hero names - albeit ridiculous ones.

Once our ArrayList is created, we print out the elements to show what values they store. After that, we use the set() method to change the value of the first element in the array - located at position "0" in this instance. Then we print out superHero to show this change actually took place. Here is the example code showing how to use the set() method in Java:

import java.util.ArrayList;
 
public class Main {
  public static void main(String[] args) {
    ArrayList superHero = new ArrayList();
    superHero.add("Big Head Man!");
    superHero.add("WhatAWoman!");
    superHero.add("Expendable Man!");
    superHero.add("Hawt Lava!");
            	System.out.println(superHero);
        	   System.out.println("Now we remove Big Head Man's head!");
        	   superHero.set(0, "No Head Man!");
    System.out.println(superHero);
  }
}

The output of the above code if you run this Java example will be:

[Big Head Man!, WhatAWoman!, Expendable Man!, Hawt Lava!]
Now we remove Big Head Man's head!
[No Head Man!, WhatAWoman!, Expendable Man!, Hawt Lava!]

Using the Get() Method in Java

The get() method in Java is used to access an element in an ArrayList array. You can then use that accessed element for any number of things. In our example, we will access an element to print it out to the user's screen. We start off by creating an ArrayList, defining its elements, and then we move onto printing out the element located at index "0". Here is the code showcasing how to use the get() method in Java:

import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    ArrayList superHero = new ArrayList();
    superHero.add("Big Head Man!");
    superHero.add("WhatAWoman!");
    superHero.add("Expendable Man!");
    superHero.add("Hawt Lava!");
            	System.out.println(superHero.get(0));
  }
}

The output from the above code will result in:

Big Head Man!

Which is the first element - or the element located at index position 0 - in the superHero array.

Using the size() Method in Java

In Java, you can use the size() method to discover how many elements there are in an ArrayList array. Once more, we start by creating an array, then we add some elements to it. Once that is completed, we use the size() method to tell us how many elements there are in the ArrayList array. Here is the code showing how to use the size() method in Java:

import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    ArrayList superHero = new ArrayList();
    superHero.add("Big Head Man!");
    superHero.add("WhatAWoman!");
    superHero.add("Expendable Man!");
	superHero.add("Hawt Lava!");
            	System.out.println("The number of Super Heroes is:");
    System.out.println(superHero.size());
  }
}

Here is the expected output for this Java code snippet:

The number of Super Heroes is:
4
James Payne
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date