[Java] Output multidimensional array / spreadsheet (AOJ⑥ spreadsheet)

StringBuilder returns a string representation

StringBuilder sb = new StringBuilder();
System.out.println(sb);
StringBuilder sb = new StringBuilder();
String result = sb.toString();//OK
String result = sb;  //Compile error(Different type)
reference

Output multidimensional array

import java.util.*;
import java.util.Arrays;
public class Main {
    public static void main(String[] args) throws Exception {
		//One-dimensional array
		int[] nums = new int[] { 1, 2, 3 };
		System.out.println(nums);//[I@36baf30c
		System.out.println(Arrays.toString(nums));// [1, 2, 3]
		//Multidimensional array
		int[][] nums2 = new int[][] { { 1, 2, 3 }, { 11, 12, 13 } };
		System.out.println(nums2);// [[I@7a81197d
		System.out.println(Arrays.toString(nums2));// [[I@5ca881b5, [I@24d46ca6]

		System.out.println(Arrays.deepToString(nums2));// [[1, 2, 3], [11, 12, 13]]
	}
}

Spreadsheet (ITP1-7)

Create a simple program to do spreadsheets. Create a program that reads a table that has elements with the number of rows r and the number of columns c and r × c, and outputs a new table with the sum of each row and column inserted. 【Input】 The first line is given r and c separated by whitespace. Each r line that follows is given c integers separated by spaces. 【Output】 Output a new table of (r + 1) × (c + 1). Separate adjacent integers on each line with a single space. Insert the total value of that row as the last column of each row, the total value of that column as the last row of each column, and the total value of the entire table in the last row / column. 【Constraints】

import java.util.Scanner;
import java.util.Arrays;
class Main {
	Scanner sc = new Scanner(System.in);

	public void run() {
		int r = sc.nextInt(), c = sc.nextInt();

		int[][] A = new int[r + 1][c + 1];

        //Scan a table with elements r x c
		for (int i = 0; i < r; i++)
			for (int j = 0; j < c; j++) {
				A[i][j] = sc.nextInt();
			}
        //Calculate the total for each column and assign it to column c
		for (int i = 0; i < r; i++) {
			int sum = 0;
			for (int j = 0; j < c; j++) {
				sum += A[i][j];
			}
			A[i][c] = sum;
		}
		//Calculate the sum for each row and assign it to column c
		for (int j = 0; j <= c; j++) {
			int sum = 0;
			for (int i = 0; i < r; i++) {
				sum += A[i][j];
			}
			A[r][j] = sum;
		}
		//Check the contents
        //System.out.println(Arrays.deepToString(A));
		
        //Join one row at a time
		for (int i = 0; i <= r; i++)
			ln(join(A[i], " "));
	}

	public static void main(String[] args) {
		new Main().run();
	}

	public static void ln(Object o) {
		System.out.println(o);
	}

    //Create a string to output with StringBuilder
	public static String join(int[] array, String separator) {
	    //The first character from the 1st row and 1st column (array)[0]) Get
		StringBuilder str = new StringBuilder(array[0] + ""); 

        //Do not output the last blank by turning from the second character
		for (int i = 1; i < array.length; i++) {
			str.append(separator).append(array[i] + "");
		}
		return str.toString();
        
        //Compile error unless converted to String with toString
        //return str; 
        //incompatible types: StringBuilder cannot be converted to String
	}
}

Recommended Posts

[Java] Output multidimensional array / spreadsheet (AOJ⑥ spreadsheet)
[Java] Multidimensional array / inner product calculation (AOJ⑦ inner product)
[Java] array
Java array
Java array
java (array)
Java array
Java array
java array
[Java] Array
[Java beginner] About initialization of multidimensional array
[Java] output, variables
java array variable
[Java] Array notes
About Java log output
Output the maximum value from the array using Java standard output
Java formatted output [Notes]
About Java Array List
[Java ~ Array ~] Study memo 4
Gzip-compress byte array in Java and output to file
Multidimensional array in Swift
Declaration of multidimensional array Let's try TypeScript for Java problem 4-4
[Java] List type / Array type conversion
[Java] Extract substrings (AOJ13 --shuffle)
Java standard log output sample
Java Development Basics ~ Exercise (Array) ~
[Java] Convert ArrayList to array
[Java Silver] Array generation method
How to initialize Java array
[Beginner] Java basic "array" description