Question

In: Computer Science

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

Solutions

Expert Solution

Hi, Please find the solution and rate the answer:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

class Scratch {
    public static void main(String[] args) {
        List<Integer> matrix1 = new LinkedList<>();
        List<Integer> matrix2 = new LinkedList<>();
        Random x = new Random();
        for (int i = 0; i < 9; i++) {
            matrix1.add(x.nextInt(100));
            matrix2.add(x.nextInt(100));
        }

        for (int i = 0; i < 9; i++) {
            System.out.print(matrix1.get(i)+"\t");

        }
        System.out.println();
        System.out.println();
        for (int i = 0; i < 9; i++) {
            System.out.print(matrix1.get(i)+" \t");
            if((i+1)%Math.sqrt(9)==0)
                System.out.println();

        }
        System.out.println();
        for (int i = 0; i < 9; i++) {
            System.out.print(matrix2.get(i) + " \t");
            if((i+1)%Math.sqrt(9)==0)
                System.out.println();
        }
        System.out.println();
        System.out.println("_________________________________");
        List<Integer> mat = add(matrix1,matrix2);
        for (int i = 0; i < mat.size(); i++) {
            System.out.print(mat.get(i)+", ");
            if((i+1)%Math.sqrt(9)==0)
                System.out.println();
        }

        System.out.println();
        System.out.println();

    }
    static List<Integer> add(List<Integer> mat1,List<Integer> mat2){
        int size=mat1.size();
        if(mat1.size()!=mat2.size())
            return null;
        List<Integer> mat = new LinkedList<>();
        for (int i = 0; i < size; i++) {
            mat.add(mat1.get(i)+mat2.get(i));
        }
        return mat;

    }
}

Output: Addition of 2 arrays in last line


Related Solutions

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. }
I am trying to add two linked-list based integers, but the program keeps giving me the...
I am trying to add two linked-list based integers, but the program keeps giving me the incorrect result. I am trying to add (List 1: 43135) + (List 2: 172). I have pasted the code below #include <iostream> using namespace std; //Linked list node class Node {    public:    int num;    Node* next; }; //Function to create a new node with given numbers Node *new_Node(int num) {    Node *newNode = new Node();    newNode->num = num;   ...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and write test cases in another java file to make sure these methods work. - Write a private method addAfter(int k, Item item) that takes two arguments, an int argument k and a data item, and inserts the item into the list after the K-th list item. - Write a method removeAfter(Node node) that takes a linked-list Node as an argument and removes the node...
(Java) Building a Doubly Linked List off of an interface I am having trouble with these...
(Java) Building a Doubly Linked List off of an interface I am having trouble with these two methods @Override public void add(T newEntry) { DoubleLinkedNode newNode = new DoubleLinkedNode(newEntry); if (!isEmpty()) { } if (isEmpty()) { first = newNode; last = newNode; } else { last.setNextNode(newNode); newNode.setPreviousNode(last); last = newNode; } // length++; numElements++; } @Override public void add(int newPosition, T newEntry) { DoubleLinkedNode newAdder = new DoubleLinkedNode(newEntry); if ((newPosition >= 0) && (newPosition <= numElements)) { numElements++; if (newPosition...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
I am having trouble figuring out how to To-Do list using java. Can anyone guide me...
I am having trouble figuring out how to To-Do list using java. Can anyone guide me through the steps for achieving this?
Using any existing 2D or 3D graphics library ( such as Java 2D, Java 3D or...
Using any existing 2D or 3D graphics library ( such as Java 2D, Java 3D or OpenGL ) draw a scene featuring various elements. You are to choose from one of the following categories: Any man-made landscape ( using Java 2D, or Java 3D, or OpenGL) Promoting a cause (Using Java 3D or OpenGL ) 3. Any visual landscape element of your choice (Using OpenGL with Java or Python ) You are free to create whatever you choose but it...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs).The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT(Do not use a class with data and operations) Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT