import java.util.*; public class VettoreSparso { private int len; private HashMap map; public VettoreSparso(int m) { len = m; map = new HashMap(); } public int size() { return len; } public void put(int i, double x) { if(i < 1 || i > len) throw new IndexOutOfBoundsException("index i out of bounds"); map.put(i, x); } public double get(int i) { if(i < 1 || i > len) throw new IndexOutOfBoundsException("index i out of bounds"); if(map.containsKey(i)) return map.get(i); return 0; } public Iterable indices() { return map.keySet(); } public double dot(VettoreSparso that) { double s = 0; for(Integer i : this.indices()) s += this.get(i) * that.get(i); return s; } public VettoreSparso plus(VettoreSparso that) { VettoreSparso v = new VettoreSparso(len); for(Integer i : this.indices()) v.put(i, this.get(i)); for(Integer i : that.indices()) v.put(i, v.get(i) + that.get(i)); return v; } public String toString() { String s = ""; for(int i=1; i<=len; i++) s = s + get(i) + " "; return s; } }