Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.13 KB | None | 0 0
  1. /*
  2. Pakeisti path
  3. butinai elem < zeros
  4.  */
  5. package sparsematrix;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.io.IOException;
  12. import java.util.LinkedList;
  13. import java.util.List;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16.  
  17. /**
  18.  *
  19.  * @author K
  20.  */
  21. public class SparseMatrix {
  22.  
  23.     /**
  24.      * @param args the command line arguments
  25.      */
  26.     public static void main(String[] args) throws FileNotFoundException, IOException {
  27.  
  28.         int matrixLength = 100;
  29.         String zeros = "0";
  30.  
  31.         //Sudedami duomenys
  32.         List<String> zodziai = gautiZodzius();
  33.         String[][] sparseMatrix = sudetiIMatrica(matrixLength, zeros, zodziai);
  34.         spausdintiMatrica(sparseMatrix, matrixLength);
  35.  
  36.         //Sudaroma sparse matrica
  37.         SparseMatrix obj = new SparseMatrix();
  38.         List<CompactMatrixElement> compactMatrix = obj.sudarytiSparseMatrica(sparseMatrix, matrixLength, zeros);
  39.  
  40.         //Spausidnama matrica, sudeta i lista
  41.         spausdintiMatricaListe(compactMatrix);
  42.  
  43.     }
  44.  
  45.     static void spausdintiMatricaListe(List<CompactMatrixElement> matrix) {
  46.         System.out.println("----Spausdinamas matricos list--------");
  47.         int i=0;
  48.         for (CompactMatrixElement e : matrix) {
  49.             System.out.println(i + " " + e);
  50.             i++;
  51.         }
  52.         System.out.println("Viso elementų List matricoje: " + matrix.size());
  53.         System.out.println("--------------------------------------");
  54.     }
  55.  
  56.     List<CompactMatrixElement> sudarytiSparseMatrica(String[][] sparseMatrix, int length, String zeros) {
  57.         List<CompactMatrixElement> compactMatrix = new LinkedList<CompactMatrixElement>();
  58.  
  59.         for (int i = 0; i < length; i++) {
  60.             for (int j = 0; j < length; j++) {
  61.                 if (sparseMatrix[i][j] != "0") {
  62.                     CompactMatrixElement newElement = new CompactMatrixElement(i, j, sparseMatrix[i][j]);
  63.                     compactMatrix.add(newElement);
  64. //                    System.out.println("ideta" + "  " + sparseMatrix[i][j]);
  65.                 }
  66.             }
  67.         }
  68.  
  69.         return compactMatrix;
  70.     }
  71.  
  72.     static void spausdintiMatrica(String[][] sparseMatrix, int length) {
  73.         for (int i = 0; i < length; i++) {
  74.             String eil = "";
  75.             for (int j = 0; j < length; j++) {
  76.                 eil += "[" + i + "]" + "[" + j + "]" + sparseMatrix[i][j] + " ";
  77.             }
  78.  
  79.             System.out.println(eil);
  80.         }
  81.         System.out.println("--------------------------------------");
  82.         System.out.println("------Duomenys sudeti i matrica-------");
  83.         System.out.println("--------------------------------------");
  84.     }
  85.  
  86.     static String[][] sudetiIMatrica(int matrixLength, String zeros, List<String> zodziai) {
  87.         String[][] sparseMatrix = new String[matrixLength][matrixLength];
  88.  
  89.         int elementas = 0;
  90.         int kurisZodis = 0;
  91.  
  92.         for (int i = 0; i < matrixLength; i++) {
  93.             for (int j = 0; j < matrixLength; j++) {
  94.                 if (elementas % 5 == 0) {
  95.                     sparseMatrix[i][j] = zodziai.get(kurisZodis);
  96.                     kurisZodis++;
  97.                 } else {
  98.                     sparseMatrix[i][j] = zeros;
  99.                 }
  100.                 elementas++;
  101.             }
  102.         }
  103.  
  104.         return sparseMatrix;
  105.     }
  106.  
  107.     static List<String> gautiZodzius() throws FileNotFoundException, IOException {
  108.         File file = new File("C:/Users/K/Desktop/SparseMatrix/src/sparsematrix/zodynas.txt");
  109.         BufferedReader br = new BufferedReader(new FileReader(file));
  110.  
  111.         List<String> zodziai = new LinkedList<String>();
  112.  
  113.         String st;
  114.         while ((st = br.readLine()) != null) {
  115.             zodziai.add(st);
  116.         }
  117.  
  118.         System.out.println("--------------------------------------");
  119.         System.out.println("sudeti zodziai i LinkedList:");
  120.         for (String zodis : zodziai) {
  121.             System.out.println(zodis);
  122.         }
  123.         System.out.println("--------------------------------------");
  124.         return zodziai;
  125.     }
  126.  
  127.     public class CompactMatrixElement {
  128.  
  129.         private int x;
  130.         private int y;
  131.         private String duom;
  132.  
  133.         public CompactMatrixElement() {
  134.         }
  135.  
  136.         public CompactMatrixElement(int x, int y, String duom) {
  137.             this.x = x;
  138.             this.y = y;
  139.             this.duom = duom;
  140.         }
  141.  
  142.         @Override
  143.         public String toString() {
  144.             return "elementas: " + "[" + x + "]" + "[" + y + "]" + duom;
  145.         }
  146.  
  147.         public void setX(int x) {
  148.             this.x = x;
  149.         }
  150.  
  151.         public void setY(int y) {
  152.             this.y = y;
  153.         }
  154.  
  155.         public void setDuom(String duom) {
  156.             this.duom = duom;
  157.         }
  158.  
  159.         //---------------------------------------------------------------------
  160.         public int getX() {
  161.             return x;
  162.         }
  163.  
  164.         public int getY() {
  165.             return y;
  166.         }
  167.  
  168.         public String getDuom() {
  169.             return duom;
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement