TransWikia.com

how to create a persistence arraylist structure in java?

Stack Overflow Asked by Pikachu Sir on February 7, 2021

I want to make a simple persistence data structure about arraylist, this is what I am thinking:

public class LinkedList<T> {
    public T element;
    
    private LinkedList(T element) {
        this.element = element;
    }
    
    private LinkedList<T> AddElememt(T element){
        var lc = new LinkedList<>();
        lc.add(element);
        return lc;
    }

This it not the correct way, how can I go to change it? Thanks!

2 Answers

Java provides java.util.ArrayList class out of box.

But you you need to implement your own array-list data structure, right? You can use the following clear implementation as example: https://howtodoinjava.com/data-structure/list-implementation-example-in-java/;

Answered by Petr Aleksandrov on February 7, 2021

Java already offers a LinkedList class. This is one of a few implementations of the List interface. ArrayList is another.

List< String > animals = new java.util.LinkedList<>() ;
animals.add( "dog" ) ;
animals.add( "cat" ) ;

If you want a non-modifiable list of a few items, use List.of.

List< String > animals = List.of( "dog" , "cat" ) ;

Answered by Basil Bourque on February 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP