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 javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

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

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

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

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

How can i print a list of arrays in JAVA?

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

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

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

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

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

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

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

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

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

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

How to convert List of arrays to two dimensional array?

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

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

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

Initialize List<> with Arrays.asList

Convert List to Array without java.util

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

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

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

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

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

Convert list of interface type to list of another class using Java 8

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