Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Pakeisti path
- butinai elem < zeros
- */
- package sparsematrix;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author K
- */
- public class SparseMatrix {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws FileNotFoundException, IOException {
- int matrixLength = 100;
- String zeros = "0";
- //Sudedami duomenys
- List<String> zodziai = gautiZodzius();
- String[][] sparseMatrix = sudetiIMatrica(matrixLength, zeros, zodziai);
- spausdintiMatrica(sparseMatrix, matrixLength);
- //Sudaroma sparse matrica
- SparseMatrix obj = new SparseMatrix();
- List<CompactMatrixElement> compactMatrix = obj.sudarytiSparseMatrica(sparseMatrix, matrixLength, zeros);
- //Spausidnama matrica, sudeta i lista
- spausdintiMatricaListe(compactMatrix);
- }
- static void spausdintiMatricaListe(List<CompactMatrixElement> matrix) {
- System.out.println("----Spausdinamas matricos list--------");
- int i=0;
- for (CompactMatrixElement e : matrix) {
- System.out.println(i + " " + e);
- i++;
- }
- System.out.println("Viso elementų List matricoje: " + matrix.size());
- System.out.println("--------------------------------------");
- }
- List<CompactMatrixElement> sudarytiSparseMatrica(String[][] sparseMatrix, int length, String zeros) {
- List<CompactMatrixElement> compactMatrix = new LinkedList<CompactMatrixElement>();
- for (int i = 0; i < length; i++) {
- for (int j = 0; j < length; j++) {
- if (sparseMatrix[i][j] != "0") {
- CompactMatrixElement newElement = new CompactMatrixElement(i, j, sparseMatrix[i][j]);
- compactMatrix.add(newElement);
- // System.out.println("ideta" + " " + sparseMatrix[i][j]);
- }
- }
- }
- return compactMatrix;
- }
- static void spausdintiMatrica(String[][] sparseMatrix, int length) {
- for (int i = 0; i < length; i++) {
- String eil = "";
- for (int j = 0; j < length; j++) {
- eil += "[" + i + "]" + "[" + j + "]" + sparseMatrix[i][j] + " ";
- }
- System.out.println(eil);
- }
- System.out.println("--------------------------------------");
- System.out.println("------Duomenys sudeti i matrica-------");
- System.out.println("--------------------------------------");
- }
- static String[][] sudetiIMatrica(int matrixLength, String zeros, List<String> zodziai) {
- String[][] sparseMatrix = new String[matrixLength][matrixLength];
- int elementas = 0;
- int kurisZodis = 0;
- for (int i = 0; i < matrixLength; i++) {
- for (int j = 0; j < matrixLength; j++) {
- if (elementas % 5 == 0) {
- sparseMatrix[i][j] = zodziai.get(kurisZodis);
- kurisZodis++;
- } else {
- sparseMatrix[i][j] = zeros;
- }
- elementas++;
- }
- }
- return sparseMatrix;
- }
- static List<String> gautiZodzius() throws FileNotFoundException, IOException {
- File file = new File("C:/Users/K/Desktop/SparseMatrix/src/sparsematrix/zodynas.txt");
- BufferedReader br = new BufferedReader(new FileReader(file));
- List<String> zodziai = new LinkedList<String>();
- String st;
- while ((st = br.readLine()) != null) {
- zodziai.add(st);
- }
- System.out.println("--------------------------------------");
- System.out.println("sudeti zodziai i LinkedList:");
- for (String zodis : zodziai) {
- System.out.println(zodis);
- }
- System.out.println("--------------------------------------");
- return zodziai;
- }
- public class CompactMatrixElement {
- private int x;
- private int y;
- private String duom;
- public CompactMatrixElement() {
- }
- public CompactMatrixElement(int x, int y, String duom) {
- this.x = x;
- this.y = y;
- this.duom = duom;
- }
- @Override
- public String toString() {
- return "elementas: " + "[" + x + "]" + "[" + y + "]" + duom;
- }
- public void setX(int x) {
- this.x = x;
- }
- public void setY(int y) {
- this.y = y;
- }
- public void setDuom(String duom) {
- this.duom = duom;
- }
- //---------------------------------------------------------------------
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public String getDuom() {
- return duom;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement