Java list

Created By: chatGPT

In Java, a List is a part of the Java Collection Framework that represents a sequence of elements. It allows duplicate values and maintains the order of insertion. The List interface is implemented by several classes, including ArrayList, LinkedList, and Vector. Here's how you can create and manipulate a List in Java.
import java.util.ArrayList;
import java.util.List;

public class ListExample {
    public static void main(String[] args) {
        // Creating a List using ArrayList
        List<String> fruits = new ArrayList<>();
        
        // Adding elements to the List
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        
        // Displaying the List
        System.out.println("Fruits List: " + fruits);
        
        // Accessing elements by index
        String firstFruit = fruits.get(0);
        System.out.println("First Fruit: " + firstFruit);
        
        // Removing an element from the List
        fruits.remove("Banana");
        System.out.println("After Removal: " + fruits);
        
        // Iterating through the List
        System.out.println("Iterating through List:");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}
The ArrayList class provides methods for manipulating the size of the list that allows resizing dynamically. You can also use the LinkedList class if your application requires frequently adding and removing elements in between. Here's a quick example of using LinkedList.
import java.util.LinkedList;
import java.util.List;

public class LinkedListExample {
    public static void main(String[] args) {
        // Creating a LinkedList
        List<String> cities = new LinkedList<>();
        
        // Adding elements to the LinkedList
        cities.add("New York");
        cities.add("Los Angeles");
        cities.add("Chicago");
        
        // Displaying the LinkedList
        System.out.println("Cities List: " + cities);
        
        // Adding an element at the beginning
        cities.add(0, "San Francisco");
        System.out.println("After Adding at Beginning: " + cities);
        
        // Removing the first element
        cities.remove(0);
        System.out.println("After Removing First Element: " + cities);
    }
}
Introduction And SetupVariablesData TypesIntFloatDoubleCharBooleanStringByteShortLongArrayListMethodVoidReturnParametersArgumentsControl FlowIf StatementElse StatementElse If StatementSwitch StatementCaseBreakContinueFor LoopWhile LoopDo While LoopEnhanced For LoopFunctionsStatic MethodInstance MethodConstructorOverloadingOverridingAccess ModifiersPrivateProtectedPackage PrivateOperatorsArithmetic OperatorsRelational OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsTernary OperatorInstanceof OperatorArrayListSetMapHashmapHashtableQueStackCommentsSingle Line CommentMulti Line CommentSyntaxSemicolonsCurly BracesParenthesesData EncapsulationInheritancePolymorphismAbstract ClassInterfaceException HandlingTry BlockCatch BlockFinally BlockThrowThrowsChecked ExceptionUnchecked ExceptionSyntax ErrorsRuntime ErrorsLogic ErrorsEvent HandlingListenersAction EventsMouse EventsKeyboard EventsWindow EventsFocus EventsKey ListenerMouse ListenerAction ListenerThreadingSynchronizationVolatile KeywordConcurrencyJava Memory Model