简体   繁体   中英

How to copy values from 2d array to doubly linked list

How can I values from 2d array to doubly linked list? I know how to do it with ArrayList but I have no idea how to implement it with doubly linked lists. How could I copy everything from 2d array to LinkedList? Do I need LinkedList of LinkedLists?

If I have

int[][] myArray = {{1,2,3}
                              {4,5,6},
                              {7,8,9}};

Then the LinkedList should look like exactly the same, ie:

[[1,2,3]
 [4,5,6],
 [7,8,9];

public void copyFromArray(int[][] myArray){

}

public class Node<Integer> {

    public Integer data;

    public Node<Integer> prev, next;

    public Node( Integer d, Node<Integer> p, Node<Integer> n ){
         this.data = d;
         this.prev = new Node();
         this.next = new Node();
    }

  }

yes, you need a LinkedList of LinkedLists:

LinkedList<LinkedList<Integer>> list = new LinkedList<>();
int[][] myArray = { {1, 2, 3}, {4,5,6},{7,8,9} };
for (int i = 0; myArray.length >= i; i++) {
        LinkedList<Integer> auxList = new LinkedList<>();
          for (int j = 0; myArray[i].length >= j; j++) {
            auxList.add(myArray[i][j]);
        }
        list.add(auxList);            
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM