Cvičenie 2 - Matice (2.10.2008 extra)
Created: 2008-10-02 - 14:56
Tento kod je taky zaciatok do extra cviceni - operacie s maticami + dorobene scitanie a nasobenie matic a formatovany vystup toString().
package Matice; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class Matica { private int[][] matica; private int pocetRiadkov; private int pocetStlpcov; Matica(int riadky, int stlpce){ matica = new int[riadky][stlpce]; pocetRiadkov = riadky; pocetStlpcov = stlpce; } Matica(File subor,int riadky, int stlpce){ matica = new int[riadky][stlpce]; pocetRiadkov = riadky; pocetStlpcov = stlpce; nacitajZoSuboru(subor); } public void nacitajZoSuboru(File suborSMaticou){ try { Scanner citac = new Scanner(suborSMaticou); int poradieRiadku = 0; while(citac.hasNextLine()){ String riadok = citac.nextLine(); String[] polozky = riadok.split(" "); for (int i = 0; i < polozky.length; i++) { matica[poradieRiadku][i]=Integer.parseInt(polozky[i]); } poradieRiadku++; } citac.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public void zapisSa(File subor){ FileWriter wr; try { wr = new FileWriter(subor); wr.write(toString()); wr.close(); } catch (IOException e) { e.printStackTrace(); } } public int getPocetRiadkov() { return pocetRiadkov; } public int getPocetStlpcov() { return pocetStlpcov; } public String toString() { int najdlhsi=0; for(int i=0;i najdlhsi) najdlhsi = s.length(); } } StringBuilder sb = new StringBuilder(); for(int i=0;i