Map<Point, BitSet> map = new LinkedHashMap<>(); try (Scanner reader = new Scanner(new File("C:/Users/File.txt"))) { for (int y = 0; reader.hasNextLine(); y++) { String[] fields = reader.nextLine().split(" "); for (int x = 0; x < fields.length; x++) { Point point = new Point(x, y); BitSet bitSet = new BitSet(); bitSet.set(Integer.parseInt(fields[x])); map.put(point, bitSet); System.out.println(point + "\t" + fields[x] + "\t" + bitSet); } } } System.out.println(map);
try (Scanner reader = new Scanner(new File("C:/Users/File.txt"))) { while (reader.hasNextLine()) { boolean[] tab = new boolean[8]; for (String number : reader.nextLine().split(" ")) tab[Integer.parseInt(number)] = true; for (boolean bit : tab) System.out.print(bit ? "1 " : "0 "); System.out.println(); } }
public interface Matrix<E> { int getWidth(); int getHeight(); E getValue(int x, int y) throws IndexOutOfBoundsException; E setValue(int x, int y, E value) throws IndexOutOfBoundsException; }
public interface BooleanMatrix extends Matrix<Boolean> { boolean getBoolean(int x, int y) throws IndexOutOfBoundsException; boolean setBoolean(int x, int y, boolean value) throws IndexOutOfBoundsException; default boolean setTrue(int x, int y) throws IndexOutOfBoundsException { return setBoolean(x, y, true); } default boolean setFalse(int x, int y) throws IndexOutOfBoundsException { return setBoolean(x, y, false); } @Override default Boolean getValue(int x, int y) throws IndexOutOfBoundsException { return getBoolean(x, y); } @Override default Boolean setValue(int x, int y, Boolean value) throws IndexOutOfBoundsException { return setBoolean(x, y, value); } }
public abstract class AbstractBooleanMatrix implements BooleanMatrix { protected final int width; protected final int height; public AbstractBooleanMatrix(int width, int height) { this.width = width; this.height = height; } @Override public final int getWidth() { return width; } @Override public final int getHeight() { return height; } protected void checkBounds(int x, int y) throws IndexOutOfBoundsException { if (x < 0 || x >= width || y < 0 || y >= height) throw new IndexOutOfBoundsException("Matrix index out of ranges: " + x + ", " + y); } public final boolean setTrue(int x, int y) { return setBoolean(x, y, true); } public final boolean setFalse(int x, int y) { return setBoolean(x, y, false); } }
public class ArrayBooleanMatrix extends AbstractBooleanMatrix { private final boolean[][] matrix; public ArrayBooleanMatrix(int width, int height) { super(width, height); matrix = new boolean[width][height]; } @Override public boolean getBoolean(int x, int y) throws IndexOutOfBoundsException { checkBounds(x, y); return matrix[x][y]; } @Override public boolean setBoolean(int x, int y, boolean value) throws IndexOutOfBoundsException { checkBounds(x, y); boolean result = matrix[x][y]; matrix[x][y] = value; return result; } }
public class Test { public static void main(String[] args) { BooleanMatrix matrix = new ArrayBooleanMatrix(2, 2); matrix.setFalse(0, 0); matrix.setTrue(0, 1); matrix.setBoolean(1, 1, matrix.getBoolean(1, 0)); } }
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.BitSet; public class Topk { public static void main(String[] args) throws IOException { //Declaration des variables BitSet b[]=new BitSet[20];// 20 number of transactions int nbl=0; int i=0; //Ouverture du fichier input File filer = new File("C:/File.txt"); BufferedReader reader = new BufferedReader(new FileReader(filer)); String line=reader.readLine(); while(line!=null) { b[i]=new BitSet(10);//10 number of items if(!line.equals("")) { String []fields = line.split(" "); for(int k=0;k<fields.length;k++) { b[i].set(Integer.parseInt(fields[k])); } } i++; line=reader.readLine(); } //write like binary Matrix for(int j=0;j<i;j++) { for(int k=0;k<10;k++) { if(b[j].get(k)) System.out.print(1+ " "); else System.out.print(0+ " "); } System.out.println(); } } }