How can I convert an array to a list without using Arrays.asList() method or Java List Interface

Crespo :

I got my Node class:

public class Node <T> {

    private Node<T> next;
    private T element;

    public void setElement(T element) {
        this.element = element; 
    }

    public T getElement() {
        return element;
    }

    public Node<T> getNext() {
        return next;
    }

    public void setNext(Node <T> next) {
        this.next= next;
    }
}

And also I have my List class:

public class List <T> {

Node<T> first;
Node<T> last;

...on which I can do various things like adding elements to my list or replacing them, erase some elements, see the size of my list etc. One example of these methods would be addFirst().

public T addFirst(T element) {

    Node<T> aux;

    if (isEmpty()) {
        aux = new Node<>();
        first= aux;
        lsat= aux;
        aux.setElement(element);
    } else {
        aux = new Node<>();
        aux.setElement(element);
        aux.setNext(first);
        first= aux;
    }

    return first.getElement();
}

I am only missing one method convert an array to a list obviously with the previous mentioned restrictions.

adxl :

One solution is to loop through the array and simply fill you List:

// your array of Nodes
Node<?>[] nodeArray = new Node[]{node1,node2,node3,node4,node5};

// your List
List<?> myList = new List<>();

for(Node<?> node : nodeArray)
{
    myList.addLast(node); // use addLast() to keep the same order as the array
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert Map<String,List<Person>> to Map<String,List<Employee>>, using Java 8 streams. I did this but how do this without for loop

What is the best way of using Arrays.asList() to initialize a List

How Arrays.asList(int[]) can return List<int[]>?

Initialize List<> with Arrays.asList

Sort a list from Arrays.asList() changes also the origin array?

How to Convert String to List<String> in Java Without using Jackson Api

List values change if I change the array passed to Arrays.asList(array)

How to convert list without generics to list with generics using streams in java?

Can I create a list of array with Arrays.asList?

How to convert an observable list to an array list? Java

How can I make an array of my arrays in Java? (WITHOUT ArrayLists)

why list can't be initialized with array, without arrays.aslist()?

How to use Arrays.asList() method to return a list of objects?

How I can convert an array value from LInkedHashMap to the List?

How can I convert a list of strings to an array of float numbers?

How can I convert a text file into a list of int arrays

Convert List to Array without java.util

How can i print a list of arrays in JAVA?

How to convert List of arrays to two dimensional array?

How can I convert a Firebase Database Snapshot into an Array List in Swift?

How can I convert this list of pairs into an object that contains an array of arrays?

How can I change a List method to an Array method?

How can I fetch arrays values in a list of items using GatsbyJS?

How can I check the arrays in an array list - Python

How can I use a list declared in two different classes in another class, without using interface in C#?

How can I convert a list of strings to list of dictionaries without using zip function?

How can I convert List of LinkedHashMap to List of Custom Object using Java 8

Trying to covert int array to List using Arrays.asList gives wrong value

Java Generics: lower bound, can add an object to a list using Arrays.asList but not using List.add

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive