Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- public class filledArray {
- public static void main(String[] args) {
- //Test for 1.Method
- Scanner input = new Scanner(System.in);
- int n = input.nextInt();
- int[][] array;
- array = genFilledArray(n);
- print(array);
- System.out.println();
- //Test for 2.Method
- array = reformatArray(new int[]{12, 145, 543});
- print(array);
- System.out.println();
- //Test for 3.Method
- array = extendArray(new int[][]{{7, 3, 5, 1, 6}, {3, 5, 6, 7, 8}, {4, 7, 3, 8, 5}});
- print(array);
- System.out.println();
- array = extendArray(new int[][]{{3, 6, 9, 5}, {2, 8, 4, 7}});
- print(array);
- System.out.println();
- array = extendArray(new int[][]{{1}});
- print(array);
- }
- public static int[][] genFilledArray(int n) {
- int[][] array = new int[n][n];
- if (n > 0) {
- for (int i = 0; i < array.length; i++) {
- for (int j = 0, help = i + 1; j < array[i].length; j++, help++) {
- array[i][j] = help;
- }
- }
- }
- return array;
- }
- public static int[][] reformatArray(int[] inputArray){
- int j = 0;
- int[][] newArray = new int[inputArray.length][inputArray.length];
- for (int i = 0; i < inputArray.length; i++){
- int n = inputArray[i];
- int count = -1;
- while (n != 0){
- int temp = n % 10;
- n /= 10;
- count++;
- newArray[j][count] = temp;
- }
- j++;
- }
- return newArray;
- }
- public static int[][] extendArray(int[][] inputArray){
- int[][] newArray = new int[inputArray[0].length][inputArray[0].length + 2];
- if (inputArray.length > 1) {
- newArray = new int[inputArray[0].length][inputArray[0].length + 2];
- }
- //case with length 1
- if (inputArray.length == 1){
- newArray = new int[inputArray[0].length + 2][inputArray[0].length + 2];
- newArray[0][0] = inputArray[0][0]; //begining
- newArray[newArray.length - 1][newArray[0].length - 1] = inputArray[0][0]; //end
- newArray[1][1] = inputArray[0][0]; //middle
- }
- boolean check = false;
- if (inputArray != null && inputArray.length > 0){
- //check if rows length equal with another is
- for (int i = 0; i < inputArray.length - 1; i++){
- if (inputArray[i].length == inputArray[i + 1].length){
- check = true;
- }else{
- check = false;
- }
- }
- if (check){
- int max = Integer.MIN_VALUE;
- int min = Integer.MAX_VALUE;
- for (int i = 0; i < inputArray.length; i++){
- for (int j = 0; j < inputArray[i].length; j++){
- if (inputArray[i][j] <= min){
- min = inputArray[i][j];
- }
- if (inputArray[i][j] >= max){
- max = inputArray[i][j];
- }
- }
- }
- //insert a min value at the begining
- newArray[0][0] = min;
- //insert a max value at the end of array
- newArray[newArray.length - 1][newArray[0].length - 1] = max;
- for (int i = 0; i < inputArray.length; i++){
- for (int j = 0; j < inputArray[i].length; j++){
- newArray[i + 1][j + 1] = inputArray[i][j];
- }
- }
- }
- }
- return newArray;
- }
- public static void print(int[][] array){
- if (array != null){
- for (int i = 0; i < array.length; i++){
- for (int j = 0; j < array[i].length; j++){
- System.out.print(array[i][j] + "\t");
- }
- System.out.println();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement