Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. public class filledArray {
  5.     public static void main(String[] args) {
  6.  
  7.         //Test for 1.Method
  8.         Scanner input = new Scanner(System.in);
  9.         int n = input.nextInt();
  10.  
  11.         int[][] array;
  12.  
  13.         array = genFilledArray(n);
  14.         print(array);
  15.         System.out.println();
  16.  
  17.         //Test for 2.Method
  18.         array = reformatArray(new int[]{12, 145, 543});
  19.         print(array);
  20.         System.out.println();
  21.  
  22.         //Test for 3.Method
  23.         array = extendArray(new int[][]{{7, 3, 5, 1, 6}, {3, 5, 6, 7, 8}, {4, 7, 3, 8, 5}});
  24.         print(array);
  25.         System.out.println();
  26.         array = extendArray(new int[][]{{3, 6, 9, 5}, {2, 8, 4, 7}});
  27.         print(array);
  28.         System.out.println();
  29.         array = extendArray(new int[][]{{1}});
  30.         print(array);
  31.  
  32.  
  33.  
  34.     }
  35.     public static int[][] genFilledArray(int n) {
  36.  
  37.         int[][] array = new int[n][n];
  38.  
  39.         if (n > 0) {
  40.             for (int i = 0; i < array.length; i++) {
  41.                 for (int j = 0, help = i + 1; j < array[i].length; j++, help++) {
  42.                     array[i][j] = help;
  43.                 }
  44.             }
  45.         }
  46.         return array;
  47.     }
  48.     public static int[][] reformatArray(int[] inputArray){
  49.  
  50.  
  51.         int j = 0;
  52.         int[][] newArray = new int[inputArray.length][inputArray.length];
  53.  
  54.         for (int i = 0; i < inputArray.length; i++){
  55.  
  56.             int n = inputArray[i];
  57.             int count = -1;
  58.  
  59.             while (n != 0){
  60.  
  61.                 int temp = n % 10;
  62.                 n /= 10;
  63.                 count++;
  64.  
  65.                 newArray[j][count] = temp;
  66.             }
  67.             j++;
  68.         }
  69.         return newArray;
  70.     }
  71.     public static int[][] extendArray(int[][] inputArray){
  72.  
  73.         int[][] newArray = new int[inputArray[0].length][inputArray[0].length + 2];
  74.  
  75.         if (inputArray.length > 1) {
  76.              newArray = new int[inputArray[0].length][inputArray[0].length + 2];
  77.         }
  78.         //case with length 1
  79.         if (inputArray.length == 1){
  80.  
  81.             newArray = new int[inputArray[0].length + 2][inputArray[0].length + 2];
  82.             newArray[0][0] = inputArray[0][0];                                          //begining
  83.             newArray[newArray.length - 1][newArray[0].length - 1] = inputArray[0][0];   //end
  84.             newArray[1][1] = inputArray[0][0];                                          //middle
  85.         }
  86.  
  87.         boolean check = false;
  88.  
  89.         if (inputArray != null && inputArray.length > 0){
  90.             //check if rows length equal with another is
  91.             for (int i = 0; i < inputArray.length - 1; i++){
  92.                 if (inputArray[i].length == inputArray[i + 1].length){
  93.                     check = true;
  94.                 }else{
  95.                     check = false;
  96.                 }
  97.             }
  98.             if (check){
  99.  
  100.                 int max = Integer.MIN_VALUE;
  101.                 int min = Integer.MAX_VALUE;
  102.  
  103.                 for (int i = 0; i < inputArray.length; i++){
  104.                     for (int j = 0; j < inputArray[i].length; j++){
  105.  
  106.                         if (inputArray[i][j] <= min){
  107.                             min = inputArray[i][j];
  108.                         }
  109.                         if (inputArray[i][j] >= max){
  110.                             max = inputArray[i][j];
  111.                         }
  112.                     }
  113.                 }
  114.                 //insert a min value at the begining
  115.                 newArray[0][0] = min;
  116.  
  117.                 //insert a max value at the end of array
  118.                 newArray[newArray.length - 1][newArray[0].length - 1] = max;
  119.  
  120.                 for (int i = 0; i < inputArray.length; i++){
  121.                     for (int j = 0; j < inputArray[i].length; j++){
  122.  
  123.                         newArray[i + 1][j + 1] = inputArray[i][j];
  124.  
  125.                     }
  126.                 }
  127.             }
  128.         }
  129.         return newArray;
  130.     }
  131.     public static void print(int[][] array){
  132.  
  133.         if (array != null){
  134.  
  135.             for (int i = 0; i < array.length; i++){
  136.                 for (int j = 0; j < array[i].length; j++){
  137.  
  138.                     System.out.print(array[i][j] + "\t");
  139.                 }
  140.                 System.out.println();
  141.             }
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement