fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Matrix<Float> m = new Matrix(2, 2);
  13. m.set(0, 0, Float.MAX_VALUE);
  14. m.set(0, 1, 42.0f);
  15. m.set(1, 0, null);
  16. m.set(1, 1, 1f);
  17. printMatrix(m);
  18. }
  19.  
  20. private static void printMatrix(Matrix<?> m) {
  21. for (int row = 0; row < m.rows; row++) {
  22. for (int col = 0; col < m.cols; col++) {
  23. System.out.println(m.get(row, col));
  24. }
  25. }
  26. }
  27.  
  28. public static class Matrix<T> {
  29.  
  30. private final Vector<Vector<T>> data;
  31. public final int rows, cols;
  32.  
  33. public Matrix(int rows, int cols) {
  34. this.rows = rows;
  35. this.cols = cols;
  36. data = new Vector<>();
  37. data.setSize(rows);
  38. for (int i = 0; i < rows; i++) {
  39. Vector<T> col = new Vector<T>();
  40. col.setSize(cols);
  41. data.set(i, col);
  42. }
  43. }
  44.  
  45. public T get(int row, int col) {
  46. return data.get(row).get(col);
  47. }
  48.  
  49. public void set(int row, int col, T val) {
  50. data.get(row).set(col, val);
  51. }
  52. }
  53. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
3.4028235E38
42.0
null
1.0