0

I'm doing a program on the addition of 2 Sparse Matrices. This is what I have coded-

import java.io.*;
class Sparse
{

    int r,c,m[][],s[][],count,rt;
    static int res[][];

    
    public Sparse()
    {
        count=0;
        rt=1;
    }
    //Take the matrix
    public void read() throws Throwable
        {
        BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the number of rows: ");
        r=Integer.parseInt(buff.readLine());
        System.out.print("Enter the number of columns: ");
        c=Integer.parseInt(buff.readLine());
        m=new int[r][c];
        
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                System.out.print("Enter the element m[" + i +"]["+ j + "]: ");
                m[i][j]=Integer.parseInt(buff.readLine());
                
                if(m[i][j] !=0)
                    count++;
            }
        }
    
    
       }
    
    
    public void display() throws Throwable
    {
        System.out.println("The matrix is as follows: ");
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                System.out.print(m[i][j]+" ");
                
            }
            System.out.println();
        }
    }
    
    
    public void displaysparse() throws Throwable
    {
        System.out.println("The sparse matrix is as follows: ");
        for(int i=0;i<rt;i++)
        {
            for(int j=0;j<3;j++)
            {
                System.out.print(s[i][j]+" ");
                
            }
            System.out.println();
        }
    }
    
    
    //Create the sparse
    public void create()
    {
        s=new int[count+1][3];
        
        s[0][0]=r;
        s[0][1]=c;
        s[0][2]=count;
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                if (m[i][j]!=0)
                {
                  s[rt][0]=i;
                  s[rt][1]=j;
                  s[rt][2]=m[i][j];
                  rt++;
                }
            }
                
            }
            
        }
    
    public static void add(Sparse a,Sparse b) throws Throwable
    {
        int temp_count=0;
        
        if (a.r== b.r && a.c==b.c)
                 
        {
            
                for(int i=1;i<a.rt;i++)
                {
            
            
                        //Getting a row of s[i][j] belonging to 'a'
                        for(int j=1;j<b.rt;j++)
                        {
                            if(a.s[i][0]==b.s[j][0] && a.s[i][1]==b.s[j][1])
                                temp_count++;
                        
                        }
            
        
                
         
        
            
                }
            //System.out.println("Common Elements: "+temp_count);
                
                res=new int[a.count+b.count-temp_count+1][3];
                res[0][0]=a.r;
                res[0][1]=a.c;
                res[0][2]=a.count+b.count-temp_count;
                int x=1;
                boolean flag;
                
                
                for(int i=1;i<a.rt;i++)
                {
                     flag=false;
            
                        //Getting a row of s[i][j] belonging to 'a'
                        for(int j=1;j<b.rt;j++)
                        {
                            if(a.s[i][0]==b.s[j][0] && a.s[i][1]==b.s[j][1])
                            {
                                res[x][0]=a.s[i][0];
                                res[x][1]=a.s[i][1];
                                res[x][2]=a.s[i][2]+b.s[j][2];
                                x++;
                                flag=true;
                            }
                        
                        }
                        
                        if(flag==false)
                        {
                            //this element of 'a' does not find a match in 'b', just put it in res
                                res[x][0]=a.s[i][0];
                                res[x][1]=a.s[i][1];
                                res[x][2]=a.s[i][2];
                                x++;
                        }
            
                }
                
                int x2;
                x2=x;
                
                //add the remaining elements of 'b'
                for(int i=1;i<b.rt;i++)
                {
                     flag=false;
            
                        //Getting a row of s[i][j] belonging to 'b'
                        for(int j=1;j<x2;j++)
                        {
                            if(b.s[i][0]==res[j][0] && b.s[i][1]==res[j][1])
                            {
                                flag=true;
                            }
                        
                        }
                        
                        if(flag==false)
                        {
                            //this element of 'b' does not find a match in res, just append in res
                                res[x][0]=b.s[i][0];
                                res[x][1]=b.s[i][1];
                                res[x][2]=b.s[i][2];
                                x++;
                        }
            
                }
                
                
                //Display RES
                
                System.out.println("The sum of the matrices is: ");
                
                for(int i=0;i<x;i++)
                {
                   for(int j=0;j<3;j++)
                   {
                    System.out.print(res[i][j]+" ");
                   }
                   System.out.println();
                }

                 
                
        }
        
        else
        {
            System.out.println("The matrices cannot be added as their dimensions do not match");
            System.exit(1);
        }
        
    }
        
        
    }
    
    



class SpMx
{
    
    public static void main(String[] args) throws Throwable
    {
        Sparse a=new Sparse();
        Sparse b=new Sparse();
        System.out.println("Entering the first matrix: ");
        a.read();
        a.display();
        a.create();
        a.displaysparse();
        
        System.out.println("Entering the second matrix: ");
        b.read();
        b.display();
        b.create();
        b.displaysparse();
        Sparse.add(a,b);
    }
    
}

As you can see from the code, the sum of a & b is held in res.

I had to go through a loop to count the number of elements that would appear in the 'res' array.

Instead of this, if I create 'res' as an ArrayList, how can I get back a 2D array from it?

2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by tyagi
0

ArrayList added to an ArrayList can be used to implement a 2D array.
Check it out.

import java.io.*;
import java.util.ArrayList;

class Sparse
{

    int r,c,m[][],s[][],count,rt;
    static ArrayList<ArrayList> res = new ArrayList<ArrayList>();
    
    public Sparse()
    {
        count=0;
        rt=1;
    }
    //Take the matrix
    public void read() throws Throwable
        {
        BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the number of rows: ");
        r=Integer.parseInt(buff.readLine());
        System.out.print("Enter the number of columns: ");
        c=Integer.parseInt(buff.readLine());
        m=new int[r][c];
        
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                System.out.print("Enter the element m[" + i +"]["+ j + "]: ");
                m[i][j]=Integer.parseInt(buff.readLine());
                
                if(m[i][j] !=0)
                    count++;
            }
        }
    
    
       }
    
    
    public void display() throws Throwable
    {
        System.out.println("The matrix is as follows: ");
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                System.out.print(m[i][j]+" ");
                
            }
            System.out.println();
        }
    }
    
    
    public void displaysparse() throws Throwable
    {
        System.out.println("The sparse matrix is as follows: ");
        for(int i=0;i<rt;i++)
        {
            for(int j=0;j<3;j++)
            {
                System.out.print(s[i][j]+" ");
                
            }
            System.out.println();
        }
    }
    
    
    //Create the sparse
    public void create()
    {
        s=new int[count+1][3];
        
        s[0][0]=r;
        s[0][1]=c;
        s[0][2]=count;
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                if (m[i][j]!=0)
                {
                  s[rt][0]=i;
                  s[rt][1]=j;
                  s[rt][2]=m[i][j];
                  rt++;
                }
            }
                
            }
            
        }
    
    public static void add(Sparse a,Sparse b) throws Throwable
    {
        int temp_count=0;
        int x=1;
        boolean flag;
        //changes done here..
        if (a.r== b.r && a.c==b.c)
        {
            ArrayList q = new ArrayList();
            q.add(a.r);
            q.add(a.c);
            res.add(q);
            for(int i=1;i<a.rt;i++)
                {
                     flag=false;
            
                        //Getting a row of s[i][j] belonging to 'a'
                        for(int j=1;j<b.rt;j++)
                        {
                            if(a.s[i][0]==b.s[j][0] && a.s[i][1]==b.s[j][1])
                            {
                                ArrayList p = new ArrayList();
                                p.add(a.s[i][0]);
                                p.add(a.s[i][1]);
                                p.add(a.s[i][2]+b.s[j][2]);
                                p.trimToSize();
                                res.add(p);
                                flag=true;
                            }
                        
                        }
                        
                        if(flag==false)
                        {
                            //this element of 'a' does not find a match in 'b', just put it in res
                                ArrayList p = new ArrayList();
                                p.add(a.s[i][0]);
                                p.add(a.s[i][1]);
                                p.add(a.s[i][2]);
                                p.trimToSize();
                                res.add(p);
                        }
            
                }
                
                
                
                //add the remaining elements of 'b'
                for(int i=1;i<b.rt;i++)
                {
                     flag=false;
            
                        //Getting a row of s[i][j] belonging to 'b'
                        for(int j=1;j<a.rt;j++)
                        {
                            if(b.s[i][0]==a.s[j][0] && b.s[i][1]==a.s[j][1])
                            {
                                flag=true;
                                break;
                            }
                        
                        }
                        
                        if(flag==false)
                        {
                            //this element of 'b' does not find a match in res, just append in res
                                ArrayList p = new ArrayList();
                                p.add(b.s[i][0]);
                                p.add(b.s[i][1]);
                                p.add(b.s[i][2]);
                                p.trimToSize();
                                res.add(p);
                        }
                }
            q.add(res.size()-1);
            q.trimToSize();
            System.out.println("Result:");
            for(ArrayList s : res)
            {
                System.out.println(s);
            }
        }
        else
        {
            System.out.println("The matrices cannot be added as their dimensions do not match");
            System.exit(1);
        }
        
    }
        
        
    }
class SpMx
{
    
    public static void main(String[] args) throws Throwable
    {
        Sparse a=new Sparse();
        Sparse b=new Sparse();
        System.out.println("Entering the first matrix: ");
        a.read();
        a.display();
        a.create();
        a.displaysparse();
        
        System.out.println("Entering the second matrix: ");
        b.read();
        b.display();
        b.create();
        b.displaysparse();
        Sparse.add(a,b);
    }
    
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.