ArrayList in Java

ArrayList in Java:

package com.zakilive;

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
	// working with arraylist

        ArrayList words=new ArrayList<>();
        words.add("hello"); //string
        words.add("there");
        words.add(9); //integer
        words.add(12.00); //double
        words.add('H'); //character

        int item1= (int) words.get(2);
        double item2= (double) words.get(3);
        String item3= (String) words.get(0);

        System.out.println(item1+item2);


    }
}

Generics:

package com.zakilive;

import java.util.ArrayList;
import java.util.LinkedList;

public class Main {

    public static void main(String[] args) {
	// working with arraylist

        ArrayList<String> words=new ArrayList<String>();
        words.add("hello"); //string
        words.add("there");
        words.add("9"); //integer
//        words.add(12.00); //double
//        words.add('H'); //character

//        int item1= (int) words.get(2);
//        double item2= (double) words.get(3);
        String item3= (String) words.get(2);
        System.out.println(item3);

//        System.out.println(item1+item2);
        LinkedList<Integer> numbers=new LinkedList<Integer>();
        numbers.add(100);
        numbers.add(200);
        numbers.removeFirst();

        for(int number: numbers)
        {
            System.out.println(number);
        }

    }
}

 

LinkedList is faster for manipulation but slower for retrieval and ArrayList is slower for manipulation but faster for retrieval

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.