Question

In: Computer Science

Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...

Java Generic 2D Linked List Problem

How to convert a 1D linked List into multiple linked lists with sequential values together?

//Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]]
//Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]]
//Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]]

public <T> List<List<T>> convert2D(List<T> list) {

// Given a 1D, need to combine sequential values together.

}

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
        //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]]
        //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]]
        //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]]
        public static <T> List<List<T>> convert2D(List<T> list) {
                
                List<List<T>> result = new ArrayList<>();
                
                T prevVal = null;
                for(int i=0; i<list.size(); i++) {
                        T val = list.get(i);
                        
                        if(i == 0 || val != prevVal) {
                                result.add(new ArrayList<>());
                                prevVal = val;
                        }
                        
                        result.get(result.size() - 1).add(val);
                }
                
                return result;
        }


        
        public static void main(String[] args) {
                System.out.println(convert2D(new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3))));
                System.out.println(convert2D(new ArrayList<>(Arrays.asList(1,1,2,1,1,2,2,2,2))));
                System.out.println(convert2D(new ArrayList<>(Arrays.asList(1,2,3,4,5))));
                
        }
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

how do you add two matrices linked list in java? (am using linked list because 2D...
how do you add two matrices linked list in java? (am using linked list because 2D arrays are not allowed.) ex [1st matrix] 1 3 2 4 2 1 3 2 4 + [2nd matrix] 3 2 3 2 1 4 5 2 3 = [3rd matrix] 4 5 5 6 3 5 8 4 7
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files. Create a class GenLLQueue which has the following: Internal class ListNode which contains: Instance variable data of type T Instance variable link of type ListNode Default constructor that sets both instance variables to null Instance Variables head which is of type ListNode which...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with class SLLNode listed as inner class. TestIntegerSLL.java that tests the SLL class by using a linked list of Integer. In SLL class add the following method:                                                                    public void moveToEnd (int i) It will move the element at the i -th position to the end of the list. You can assume i to be within the list, and that the first element has the...
How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
How do I convert the integers in my 2D array list to output decimals numbers? My...
How do I convert the integers in my 2D array list to output decimals numbers? My program works accepting regular integers, but crashes when decimals are entered into my 2D array list. Can someone improve it so it accepts decimal numbers? Here is my code: import javax.swing.*; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class TwoDimArray { private int numbers[][]; public TwoDimArray() { loadArray(); } public void loadArray() { /* //loadArray() method loads the users defined filename //@return returns the...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
2D and 3D Motions How does one use the rules for solving 1D motion problems and...
2D and 3D Motions How does one use the rules for solving 1D motion problems and apply them to 2D or 3D motion problems? What is a major difference between velocity and acceleration vectors when it comes to their directions relative to the motion of an object? In general, how would you describe the horizontal speed of an object during projectile motion?
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT