sparse vector

设计sparse vector

sparseVector v = new sparseVector(100); //size constructor; size is 100.
    v.set(0, 1.0);
    v.set(3, 2.0);
    v.set(80,-4.5);

    System.out.println(v.get(80)); //should print -4.5
    System.out.println(v.get(50)); //should print 0.0

    try {
       System.out.println(v.get(101)); //error -- index out of range
       throw new IllegalStateException("We should not get here, an exception should have been thrown");
    } catch (IndexOutOfBoundsException t) {
       // success
    }
    System.out.println(v.toString()); //should print something like [1.0, 0.0, 0.0, 2.0, 0.0, ...]  

https://www.geeksforgeeks.org/implementing-sparse-vector-in-java/

Last updated