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.  
  11. public static class Matrix<T> {
  12.  
  13. private ArrayList<T> array;
  14. private int rows;
  15. private int columns;
  16.  
  17. public Matrix(int rows, int columns) {
  18. this.array = new ArrayList<T>(Collections.nCopies(rows * columns, (T)null));
  19. this.rows = rows;
  20. this.columns = columns;
  21. }
  22.  
  23. public T get(int row, int col) {
  24. return this.array.get(row * this.columns + col);
  25. }
  26.  
  27. public T set(int row, int col, T n) {
  28. return this.array.set(row * this.columns + col, n);
  29. }
  30.  
  31. public Stripe row(int row) { return this.new Stripe(row, true); }
  32. public Stripe column(int col) { return this.new Stripe(col, false); }
  33.  
  34. public class Stripe implements List<T> {
  35.  
  36. private class StripeIterator implements ListIterator<T> {
  37. private int curr;
  38. private int from;
  39.  
  40. public StripeIterator() { this(0); }
  41. public StripeIterator(int from) { this.curr = from; this.from = from; }
  42. public void add(T e) { throw new UnsupportedOperationException(); }
  43. public boolean hasNext() { return this.curr < Stripe.this.size(); }
  44. public boolean hasPrevious() { return this.curr > this.from; }
  45. public T next() { return Stripe.this.get(curr++); }
  46. public int nextIndex() { return this.curr + 1; }
  47. public T previous() { return Stripe.this.get(curr--); }
  48. public int previousIndex() { return this.curr + 1; }
  49. public void remove() { throw new UnsupportedOperationException(); }
  50. public void set(T n) { Stripe.this.set(this.curr - 1, n); }
  51. }
  52.  
  53. private int i;
  54. private boolean isRow;
  55. private int from;
  56. private int to;
  57.  
  58. private Stripe(int i, boolean isRow) {
  59. this(i, isRow, 0, 0);
  60. this.to = (isRow) ? Matrix.this.columns : Matrix.this.rows;
  61. }
  62.  
  63. private Stripe(int i, boolean isRow, int from, int to) {
  64. this.i = i;
  65. this.isRow = isRow;
  66. this.from = from;
  67. this.to = to;
  68. }
  69.  
  70. // Operations that modify the size are now allowed by the contract of
  71. // this class. In fact they are optional as far as `List` is concerned.
  72. public boolean add(T e) { throw new UnsupportedOperationException(); }
  73. public void add(int i, T e) { throw new UnsupportedOperationException(); }
  74. public boolean addAll(Collection<? extends T> c)
  75. public boolean addAll(int index, Collection<? extends T> c)
  76. public void clear() { throw new UnsupportedOperationException(); }
  77. public T remove(int index) { throw new UnsupportedOperationException(); }
  78. public boolean remove(Object o) { throw new UnsupportedOperationException(); }
  79. public boolean removeAll(Collection<?> c) { throw new UnsupportedOperationException(); }
  80. public boolean retainAll(Collection<?> c) { throw new UnsupportedOperationException(); }
  81.  
  82. public boolean contains(Object o) {
  83. for (int c = 0; c < this.size(); c++)
  84. if (this.get(c).equals(o))
  85. return true;
  86. return false;
  87. }
  88.  
  89. public boolean containsAll(Collection<?> c) {
  90. for (Object e : c)
  91. if (!this.contains(e))
  92. return false;
  93. return true;
  94. }
  95.  
  96. public T get(int k) {
  97. if (this.isRow) return Matrix.this.get(this.i, k + this.from);
  98. else return Matrix.this.get(k + this.from, this.i);
  99. }
  100.  
  101. public int hashCode() {
  102. int hashCode = 1;
  103. for (T e : this)
  104. hashCode = 31 * hashCode + (e == null ? 0 : e.hashCode());
  105. return hashCode;
  106. }
  107.  
  108. public int indexOf(Object o) {
  109. for (int j = 0; j < this.size(); j++)
  110. if (this.get(j).equals(o))
  111. return j;
  112. return -1;
  113. }
  114.  
  115. public boolean isEmpty() {
  116. return false;
  117. }
  118.  
  119. public Iterator<T> iterator() {
  120. return this.listIterator();
  121. }
  122.  
  123. public ListIterator<T> listIterator() {
  124. return this.new StripeIterator();
  125. }
  126.  
  127. public ListIterator<T> listIterator(int from) {
  128. return this.new StripeIterator(from);
  129. }
  130.  
  131. public int lastIndexOf(Object o) {
  132. int last = -1;
  133. for (int j = 0; j < this.size(); j++)
  134. if (this.get(j).equals(o))
  135. last = j;
  136. return last;
  137. }
  138.  
  139. public T set(int k, T n) {
  140. if (this.isRow) return Matrix.this.set(this.i, k + this.from, n);
  141. else return Matrix.this.set(k + this.from, this.i, n);
  142. }
  143.  
  144. public int size() { return this.to - this.from; }
  145.  
  146. public List<T> subList(int fromIndex, int toIndex) {
  147. return new Stripe(this.i, this.isRow, fromIndex, toIndex);
  148. }
  149.  
  150. public Object[] toArray() {
  151. Object[] arr = new Object[this.size()];
  152. for (int i = 0; i < this.size(); i++)
  153. arr[i] = this.get(i);
  154. return arr;
  155. }
  156.  
  157. public <E> E[] toArray(E[] arr) {
  158. if (arr.length < this.size()) {
  159. return (E[]) this.toArray();
  160. } else {
  161. for (int i = 0; i < this.size(); i++)
  162. arr[i] = (E) this.get(i);
  163. for (int i = this.size(); i < arr.length; i++)
  164. arr[i] = null;
  165. return arr;
  166. }
  167.  
  168. }
  169.  
  170. }
  171.  
  172. }
  173.  
  174. public static void main (String[] args) throws java.lang.Exception
  175. {
  176. Matrix<String> x = new Matrix<String>(2, 5);
  177. x.set(0, 0, "1");
  178. x.set(0, 1, "2");
  179. x.set(0, 2, "3");
  180. x.set(0, 3, "4");
  181. x.set(0, 4, "5");
  182. x.set(1, 0, "6");
  183. x.set(1, 1, "7");
  184. x.set(1, 2, "8");
  185. x.set(1, 3, "9");
  186. x.set(1, 4, "10");
  187.  
  188. System.out.print("Row 0\n\t");
  189. for (String a : x.row(0))
  190. System.out.print(a + " ");
  191. System.out.print("\nRow 1\n\t");
  192. for (String b : x.row(1))
  193. System.out.print(b + " ");
  194. System.out.print("\nCol 0\n\t");
  195. for (String b : x.column(0))
  196. System.out.print(b + " ");
  197. }
  198. }
Success #stdin #stdout 0.06s 380160KB
stdin
Standard input is empty
stdout
Row 0
	1 2 3 4 5 
Row 1
	6 7 8 9 10 
Col 0
	1 6