Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Quearn

Quearn Logo Quearn Logo

Quearn Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Quearn Academy
  • About Us
  • Blog
  • Contact Us
Home/Questions/Q 803879
Next
In Process
codemania
  • 0
codemania
Asked: May 27, 20232023-05-27T09:24:03+00:00 2023-05-27T09:24:03+00:00In: Education

Java Program to Implement Sparse Array

  • 0
Java Program to Implement Sparse Array
codejava
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    1. codemania
      2023-05-27T09:24:16+00:00Added an answer on May 27, 2023 at 9:24 am
      This Java program is to Implement Sparse array. In computer science, a sparse array is an array in which most of the elements have the same value (known as the default value—usually 0 or null). The occurrence of zero elements in a large array is inefficient for both computation and storage. An array in which there is a large number of zero elements is referred to as being sparse.
      Here is the source code of the Java program to implement sparse array. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.
      1. class List
      2. {
      3.     private int index;
      4.     private Object value;
      5.     private List nextindex;
      6.     public List(int index)
      7.     {
      8.         this.index = index;
      9.         nextindex = null;
      10.         value = null;
      11.     }
      12.     public List()
      13.     {
      14.         index = -1;
      15.         value = null;
      16.         nextindex = null;
      17.     }
      18.     public void store(int index, Object value)
      19.     {
      20.         List current = this;
      21.         List previous = null;
      22.         List node = new List(index);
      23.         node.value = value;
      24.         while (current != null && current.index < index)
      25.         {
      26.             previous = current;
      27.             current = current.nextindex;
      28.         }
      29.         if (current == null)
      30.         {
      31.             previous.nextindex = node;
      32.         } else
      33.         {
      34.             if (current.index == index)
      35.             {
      36.                 System.out.println("DUPLICATE INDEX");
      37.                 return;
      38.             }
      39.             previous.nextindex = node;
      40.             node.nextindex = current;
      41.         }
      42.         return;
      43.     }
      44.     public Object fetch(int index)
      45.     {
      46.         List current = this;
      47.         Object value = null;
      48.         while (current != null && current.index != index)
      49.         {
      50.             current = current.nextindex;
      51.         }
      52.         if (current != null)
      53.         {
      54.             value = current.value;
      55.         } else
      56.         {
      57.             value = null;
      58.         }
      59.         return value;
      60.     }
      61.     public int elementCount()
      62.     {
      63.         int elementCount = 0;
      64.         for (List current = this.nextindex; (current != null); current = current.nextindex)
      65.         {
      66.             elementCount++;
      67.         }
      68.         return elementCount;
      69.     }
      70. }
      71. public class SparseArray
      72. {
      73.     private List start;
      74.     private int index;
      75.     SparseArray(int index)
      76.     {
      77.         start = new List();
      78.         this.index = index;
      79.     }
      80.     public void store(int index, Object value)
      81.     {
      82.         if (index >= 0 && index < this.index)
      83.         {
      84.             if (value != null)
      85.                 start.store(index, value);
      86.         } else
      87.         {
      88.             System.out.println("INDEX OUT OF BOUNDS");
      89.         }
      90.     }
      91.     public Object fetch(int index)
      92.     {
      93.         if (index >= 0 && index < this.index)
      94.             return start.fetch(index);
      95.         else
      96.         {
      97.             System.out.println("INDEX OUT OF BOUNDS");
      98.             return null;
      99.         }
      100.     }
      101.     public int elementCount()
      102.     {
      103.         return start.elementCount();
      104.     }
      105.     public static void main(String... arg)
      106.     {
      107.         Integer[] iarray = new Integer[5];
      108.         iarray[0] = 1;
      109.         iarray[1] = null;
      110.         iarray[2] = 2;
      111.         iarray[3] = null;
      112.         iarray[4] = null;
      113.         SparseArray sparseArray = new SparseArray(5);
      114.         for (int i = 0; i < iarray.length; i++)
      115.         {
      116.             sparseArray.store(i, iarray[i]);
      117.         }
      118.         System.out.println("NORMAL ARRAY");
      119.         for (int i = 0 ; i < iarray.length; i++)
      120.         {
      121.             System.out.print(iarray[i] + "\t");
      122.         }
      123.         System.out.println("\nSPARSE ARRAY");
      124.         for (int i = 0; i < iarray.length; i++)
      125.         {
      126.             if (sparseArray.fetch(i) != null)
      127.                 System.out.print(sparseArray.fetch(i) + "\t");
      128.         }
      129.         System.out.println("The Size of Sparse Array is " + sparseArray.elementCount());
      130.     }
      131. }
      $javac SparseArray.java
      $java SparseArray
       
      NORMAL ARRAY
      1	null	2	null	null	
      SPARSE ARRAY
      1	2	
      The Size of Sparse Array 2
      • 0
      • Share
        Share
        • Share onFacebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Related Questions

    • Codility Passing Car Problem in Java
    • Dutch National Flag Problem in Java
    • Java Program to Check if an Array is Decreasing
    • Java Program to Check if an Array is Increasing
    • Smallest Sum Contiguous Subarray in Java

    Sidebar

    Ask A Question
    • Popular
    • Answers
    • imran

      Point out the wrong statement. A. The standard instances are ...

      • 217 Answers
    • imran

      Point out the correct statement. A. A volume is mounted ...

      • 204 Answers
    • VARISHA PARVEZ

      Which type of e‐commerce focuses on consumers dealing with ...

      • 180 Answers
    • codemania
      codemania added an answer Answer: b Explanation: In UNIX, a new process is created… May 28, 2023 at 10:55 am
    • Shivangi
      Shivangi added an answer C.tqm May 28, 2023 at 10:55 am
    • Varsha
      Varsha added an answer C. IBM 1401 May 28, 2023 at 10:55 am

    Related Questions

    • . In Unix, which system call creates the new process? ...

      • 1 Answer
    • What are the types of distributed operating systems? a) Zone ...

      • 0 Answers
    • Network operating system runs on ___________ a) every system in ...

      • 1 Answer
    • Which principle states that programs, users, and even the systems ...

      • 1 Answer
    • In SCSI disks used in high end PCs, the controller ...

      • 1 Answer

    Top Members

    Siya08

    Siya08

    • 0 Questions
    • 0 Answers
    Tanvi R

    Tanvi R

    • 0 Questions
    • 0 Answers
    Dhanashri

    Dhanashri

    • 0 Questions
    • 0 Answers

    Trending Tags

    account algorithm analytics answer bank c computer computer science python cs e commerce electric electronics it language mcq mechanical physics programming python question

    Explore

    • Home
    • Quearn Academy
    • Add group
    • Groups page
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help

    Footer

    Quearn

    Quearn is a social questions & Answers Engine which will help you establish your community and connect with other people, Also you Can Earn Money.

    About Us

    • Meet The Team
    • Blog
    • About Us
    • Contact Us

    Legal Stuff

    • Terms of Use
    • Privacy Policy
    • Cookie Policy

    Help

    • Knowledge Base
    • Support

    Follow

    © 2018-2023 Quearn®. All Rights Reserved | Developed by ASHAS Industries Proudly 🇮🇳