Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.mrpaul.mh180.ps12;
- /**
- * Makes 2d Array methods
- * This program creates certain methods to be run on and using 2-dimensional arrays.
- * <p>
- * ADS PS12: Sort It Out
- * 2/22/18
- *
- * @author Dhruv Rajagopal
- */
- public class Exercises2dArr {
- //Main method, makes sample 2d arrays and runs methods on them.
- public static void main(String[] args) {
- int[][] a1 = {{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}};
- int[] arr = collapseRows(a1);
- for(int i = 0; i < arr.length; i++) {
- System.out.println(arr[i]);
- }
- int[][] a2 = generateRagged(3);
- for(int i = 0; i < a2.length; i++) {
- for(int j = 0; j < a2[i].length; j++) {
- System.out.println(a2[i][j]);
- }
- System.out.println();
- }
- }
- public static int[] collapseRows(int[][] a) {
- int[] sum = new int[a.length];
- for(int i = 0; i < a.length; i++) {
- for(int j = 0; j < a[i].length; j++) {
- sum[i] += a[i][j];
- }
- }
- return sum;
- }
- public static int[][] generateRagged(int n){
- int count = 0;
- int[][] ragged = new int[n][];
- for(int i = 0; i < ragged.length; i++) {
- ragged[i] = new int[i + 1];
- for(int j = 0; j < i + 1; j++) {
- count++;
- ragged[i][j] = count;
- }
- }
- return ragged;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement