Giter VIP home page Giter VIP logo

javase-30.lists's Introduction

JavaSE-Lists

See the Udemy course: https://www.udemy.com/course/curso-certificacion-profesional-desarrollador-java-se-11

image

Let's talk about ArrayList, Vector, and LinkedList in Java.

1. ArrayList:

ArrayList is a part of the Java Collections Framework and is implemented in the java.util package.

It's a resizable array that can dynamically grow or shrink in size.

It provides fast random access to elements, making it efficient for retrieving elements by index.

import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<String> arrayList = new ArrayList<>();

        // Adding elements
        arrayList.add("Apple");
        arrayList.add("Banana");
        arrayList.add("Orange");

        // Accessing elements
        System.out.println(arrayList.get(1)); // Output: Banana
    }
}

2. Vector:

Vector is also a part of the Java Collections Framework and is similar to ArrayList.

It's synchronized, which means it's thread-safe. This can be an advantage in multithreaded environments, but it might introduce some performance overhead.

import java.util.Vector;

public class VectorExample {
    public static void main(String[] args) {
        // Creating a Vector
        Vector<String> vector = new Vector<>();

        // Adding elements
        vector.add("Red");
        vector.add("Green");
        vector.add("Blue");

        // Accessing elements
        System.out.println(vector.get(2)); // Output: Blue
    }
}

3. LinkedList

LinkedList is another implementation of the List interface, and it's based on a doubly-linked list data structure.

It provides efficient insertion and deletion of elements, especially in the middle of the list, but it may not be as efficient for random access.

import java.util.LinkedList;

public class LinkedListExample {
    public static void main(String[] args) {
        // Creating a LinkedList
        LinkedList<String> linkedList = new LinkedList<>();

        // Adding elements
        linkedList.add("Cat");
        linkedList.add("Dog");
        linkedList.add("Fish");

        // Accessing elements
        System.out.println(linkedList.get(1)); // Output: Dog
    }
}

Each of these data structures has its use cases, and the choice between them depends on the specific requirements of your program.

If you need fast random access, ArrayList is a good choice. If you need synchronization, Vector might be suitable.

If you require efficient insertion and deletion, especially in the middle of the list, LinkedList is worth considering.

javase-30.lists's People

Contributors

luiscoco avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.