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.
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?
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...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create a generic class called GenLinkedList. GenLinkedList will use nodes that store a value of the generic type to store its contents. It should have the following methods. The methods should all operate on the object making the call (none are static). Perform checking of the parameters and throw exceptions where appropriate. The linked list should be singly-linked. It should not use sentinel nodes (empty...
In JAVA How can you multiply two matrices that are different lengths. the answer should be...
In JAVA How can you multiply two matrices that are different lengths. the answer should be in 3d array so [][][] how to multiply a 3d array with a 2d array and the result is in 3d array for example: double[][][] nums1 = { { {0.0, 0.1, 2.0}, {3.0,4.0,5.0} }, { {6.0,7.0,8.0}, {9.0,10.0,11.0} } } int [][] nums2 = { {1,2,3}, {4,5,6}, {7,8,9}} and the results should be in [][][] <-- in this dimension int[][][] answer = { {  {18, 21,24},...
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
How do you write an append function for a linked list that is a that has...
How do you write an append function for a linked list that is a that has a O(1) Big O notation in Python? The one given in class is O(n). He recommended using a variable to track the end the list. This is the code I have written so far. I don't think I'm properly defining the tail in the add possibly class Node: def __init__(self, init_data): self.data = init_data self.next = None def get_data(self): return self.data def get_next(self): return...
Hi, I would like to test a java program. I am learning linked list and going...
Hi, I would like to test a java program. I am learning linked list and going to make a linked lists for integer nodes. For instance, I am going to add the numbers 12, 13, and 16 to the list and then display the list contents and add 15 to the list again and display the list contents and delete 13 from the list and display the list contents and lastly delete 12 from the list and display the list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT