Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- public class Matrix
- {
- int lines;
- int columns;
- List<Integer> values = new ArrayList<Integer>();
- public int getCountElements()
- {
- return lines * columns;
- }
- public int getLines()
- {
- return lines;
- }
- public int getColumns()
- {
- return columns;
- }
- public void initZeroMatrix()
- {
- int elements = getCountElements();
- for (int i = 0; i<elements; i++)
- {
- values.add(0);
- }
- }
- public void creatRandomValues()
- {
- Random rnd = new Random();
- for (int i = 0;i < values.size(); i++)
- {
- values.set(i, rnd.nextInt(20));
- }
- }
- public boolean isSquare()
- {
- if (this.lines == this.columns)
- return true;
- else
- return false;
- }
- Matrix(int lines, int columns)
- {
- this.lines = lines;
- this.columns = columns;
- initZeroMatrix();
- System.out.println("Создана матрица с " + lines + " строк и с " + columns + " столбцов.");
- System.out.println("");
- }
- public int getValuesElement(int index)
- {
- return values.get(index);
- }
- public void addVariablesForElement(int index, int add)
- {
- this.values.set(index, this.values.get(index) + add);
- }
- public void addVariablesForIndex(String index, int add)
- {
- int place = getElement(index, false);
- this.values.set(place, this.values.get(place) + add);
- }
- public void setVariablesForElement(int index, int value)
- {
- this.values.set(index, value);
- }
- public void setVariablesForIndex(String index, int value)
- {
- int place = getElement(index, false);
- this.values.set(place, value);
- }
- public int getElementIndex(String index)
- {
- return getElement(index, false);
- }
- public int getElementValue(String index)
- {
- return getElement(index, true);
- }
- public int getElement(String index, boolean valuesB)
- {
- String[] values = index.split(",");
- int line = Integer.parseInt(values[0]);
- int columns = Integer.parseInt(values[1]);
- int indexEl = line * this.columns - (this.columns - columns) - 1;
- if (valuesB)
- return this.values.get(indexEl);
- else
- return indexEl;
- }
- public String getIndexElement(int number)
- {
- int columns = 1;
- int line = 1;
- for (int i = 0; i<number; i++)
- {
- if (columns == lines)
- {
- line++;
- columns = 0;
- }
- columns++;
- }
- return (line + "," + columns);
- }
- public void showInfoMatrix()
- {
- System.out.println("");
- System.out.println("================================");
- if (isSquare())
- System.out.println("Матрица квадратна");
- System.out.println("Матрица состоит из " + lines + " строк.");
- System.out.println("Матрица состоит из " + columns + " столбцов.");
- System.out.println("================================");
- System.out.println("");
- }
- public void showMatrix()
- {
- int elements = getCountElements();
- int steps = 0;
- System.out.println("");
- for (int i = 0; i<elements; i++)
- {
- if (steps == 0)
- System.out.print(" || ");
- System.out.print(getValuesElement(i) + "(" + getIndexElement(i) + ") || ");
- steps++;
- if (steps == lines)
- {
- steps = 0;
- System.out.println("");
- }
- }
- System.out.println("");
- }
- }
Add Comment
Please, Sign In to add comment