import java.util.LinkedList; public class SwapStore2 implements SwapStoreInterface { LinkedList>> core = new LinkedList>>(); @Override public void set(String[][] words) { for (int i = 0; i < words.length; i++) { LinkedList tmp = new LinkedList(); for (int j = 0; j < words[i].length; j++) { tmp.add(words[i][j]); } LinkedList> origShift = new LinkedList>(); origShift.add(tmp); core.add(origShift); } } @Override public int[] lineIndices(int line) { int[] result = new int[core.get(line).size() * core.get(line).get(0).size()]; for (int i = 0; i < core.get(line).size(); i++) { for (int j = 0; j < core.get(line).get(i).size(); j++) { result[(i+1)*(j+1)-1] = j; } } return result; } @Override public void setShifts(int line, int[][] shiftedIndices) { LinkedList> tmp = new LinkedList>(); for (int i = 0; i < shiftedIndices.length; i++) { tmp.add(new LinkedList()); for (int j = 0; j < shiftedIndices[i].length; j++) { tmp.get(i).add(core.get(line).get(0).get(shiftedIndices[i][j])); } } core.set(line, tmp); } @Override public void swap(int i, int j, int k) { LinkedList tmp = core.get(i).get(j); core.get(i).set(j, core.get(i).get(k)); core.get(i).set(k, tmp); } @Override public int compare(int i, int j, int k) { int result = 0; int w = 0; // w for word while (core.get(i).size() > 0 && result == 0 && w < Math.min(core.get(i).get(j).size(), core.get(i).get(k).size())) { result = core.get(i).get(j).get(w).compareTo(core.get(i).get(k).get(w)); w++; } return result; } @Override public int length() { return core.size(); } @Override public int lineLength(int i) { return core.get(i).size(); } @Override public String get() { String result = ""; for (LinkedList> line : core) { for (LinkedList shift : line) { for (String word : shift) { result += word + ' '; } result += ", "; } result += '\n'; } return result; } }