Selasa, 21 April 2015

JAVA - How To Use ArrayList In Java NetBeans

Java - How To Display Data From An ArrayList In Java

                                                                                                       

In this java Collection tutorial we will see How To Use An ArrayList.
1: how to create a ArrayList 
2: how to dispaly data from it
3: how to clear all elements from it
And More.......


*Java Courses Java Complete Course
                            Java Swing Course

Source Code:

package javaapp;
import java.util.ArrayList;
public class Woirk {

    public static void main(String[] args){
     
       ArrayList list = new ArrayList();

       //add data to the ArrayList

       list.add(1);
       list.add("string");
       list.add('c');
       list.add(false);
       list.add(10.23f);
       list.add(null);
       
      System.out.println("display data from Arraylist");
      
      displaydata(list); 
      
     System.out.println("______ Remove elements from ArrayList");
       
      list.remove(3);
      displaydata(list); 
      
      System.out.println("________ Clear All elements from ArrayList");
      
      list.clear(); 
      displaydata(list); 
    }

    //create a methode to dispaly values from an arraylist
    public static void displaydata(ArrayList l){
         for(int i = 0; i < l.size(); i++){
           System.out.println(l.get(i));
       }
    }
}


////////////// OUTPUT:

display data from Arraylist
1
string
c
false
10.23
null
______ Remove elements from ArrayList
1
string
c
10.23
null
________ Clear All elements from ArrayList




Another Source Code:

package javaapp;

import java.util.List;
import java.util.ArrayList;

public class Woirk {
       
public static void main(String[] args){

    ArrayList list = new ArrayList();
    
    ArrayList<Integer> list2 = new ArrayList<Integer>();
    list2.add(100);
    list2.add(200);
    list2.add(300);
    list2.add(400);
    list2.add(500);
    
    //add elements to the list
    for(int i = 0; i < 10; i++){
        list.add(i);
    }
    
    //add element to a specific position in ArrayList
   list.add(list.size()-1,99999);
   
   /*
   Insert all elements in The liste2 in list 
   starting at the specific positionc (in this example 5)
   */
   list.addAll(5,list2);
   
   //display ArrayList data
   System.out.println(list);

   //cast ArrayList to Object[]
   Object[] obj = list.toArray();
   System.out.println("Start Of ToArray");
   for(Object o : obj)
   System.out.print(" - "+o);
   System.out.println("\n End Of ToArray");
   
   //clone or copy the list elements to another list
    ArrayList li = new ArrayList();
    li = (ArrayList<Integer>) list.clone();
    
     System.out.println("The List Clone Start.......");
    
        System.out.println(li);
    
    System.out.println("The List Clone End.......");
    
    /*
    get a sublist between to index 
    fromIndex inclusive, and toIndex exclusive
    */
    List<Integer> sublist = list.subList(10, 13);
    
    System.out.println("The subList Start.......");

        System.out.println(sublist);
     
    System.out.println("The SubList End.......");   
       
    // return the index of the element
    //-1 if not exist
    int index = list.indexOf(99999);
    System.out.println("The Index Of 99999 Is: "+ index);
    
    //if the element exist = true, if not = false
    boolean test = list.contains(300);
    System.out.println("the list contains 300? => "+test);
    
    //clear all the elemnts in the arraylist

    System.out.println("list size before the clear: "+list.size());
    list.clear();
    System.out.println("list size after the clear: "+list.size());
    
    //see if the list is empty
    System.out.println("Check if the list is empty: "+list.isEmpty());
  }

}


////////////// OUTPUT:

[0, 1, 2, 3, 4, 100, 200, 300, 400, 500, 5, 6, 7, 8, 99999, 9]
Start Of ToArray
 - 0 - 1 - 2 - 3 - 4 - 100 - 200 - 300 - 400 - 500 - 5 - 6 - 7 - 8 - 99999 - 9
 End Of ToArray
The List Clone Start.......
[0, 1, 2, 3, 4, 100, 200, 300, 400, 500, 5, 6, 7, 8, 99999, 9]
The List Clone End.......
The subList Start.......
[5, 6, 7]
The SubList End.......
The Index Of 99999 Is: 14
the list contains 300? => true
list size before the clear: 16
list size after the clear: 0

Check if the list is empty: true


Bagikan

Jangan lewatkan

JAVA - How To Use ArrayList In Java NetBeans
4/ 5
Oleh

Subscribe via email

Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.