Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.IOException;
- import java.time.LocalTime;
- import java.time.format.DateTimeFormatter;
- import java.util.*;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- //Ако искаме да четем амтрица, която е репрезентирана от списък от списъци от Ineger,
- //за да можем лесно да вадим елементи, което от масив е трудно а от List е лесно
- List<List<Integer>> matrix = readMatrix(scanner);
- }
- private static List<List<Integer>> readMatrix(Scanner scanner) {
- int[] rowsAndCols = Arrays.stream(scanner.nextLine().split(" "))
- .mapToInt(Integer::parseInt)
- .toArray();
- int rows = rowsAndCols[0];
- int cols = rowsAndCols[1];
- //int [][] matrix=new int[rows][cols]; тук е разликата когато искаме arrayList:
- List<List<Integer>> matrix = new ArrayList<>();
- for (int i = 0; i < rows; i++) {
- // и тук е другата разлика:
- // matrix[i]= Arrays.stream(scanner.nextLine().split(" "))
- // .mapToInt(Integer::parseInt)
- // .toArray();
- List<Integer> list = Arrays.stream(scanner.nextLine().split(" "))
- .map(Integer::parseInt)
- .collect(Collectors.toList());
- matrix.add(list);//така си четем редовете
- }
- return matrix;
- }
- }
Add Comment
Please, Sign In to add comment