ADVERTISEMENT
ADVERTISEMENT

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

ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.