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