Java - Example of ArrayList with String and Integer.


R.N. 14 September 2016

In this code snippet/program we will learn how to make an array of Strings and Integers using ArrayList in Java?

In this example we are going to create two Arrays: Strings Array and Integers Array using Java ArrayList. And print both arrays using for each loop in java.

Java Code Snippet - Example of ArrayList with String and Integer

package com.includehelp;

import java.util.ArrayList;

/**
 *
 * @author IncludeHelp
 */
public class ArrayListExample {
    public static void main(String[] args) {
        
        //Declare Array List which contain only String
        ArrayList<String> player = new ArrayList();  
        
        ///Add String item in Array List
        player.add("Sachin");
        player.add("Saurav");
        player.add("Rahul");
        player.add("Virendar");
        player.add("Anil");
        player.add("Zaheer");
        
         //Declare Array List which contain only int
         ArrayList<Integer> score = new ArrayList();  
        
        ///Add int item in Array List
        score.add(100);
        score.add(200);
        score.add(300);
        score.add(400);
        score.add(500);
        
        //Traverse player Array List
        for(String name:player){
           System.out.println(name); //Print List Item
        }
        
         //Traverse score Array List
        for(int sc:score){
           System.out.println(sc); //Print List Item
        }
    }
}
    

    Sachin
    Saurav
    Rahul
    Virendar
    Anil
    Zaheer

    100
    200
    300
    400
    500



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.