Advertisement
Guest User

Javaproject

a guest
Feb 12th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. package arrayQuiz1;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5.  
  6. public class Quiz1 {
  7.  
  8.     public static void main(String[] args) {
  9.         int[] sorted = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  10.         int[] unSorted = { 3, 5, 6, 7, 8, 9, 2, 1 };
  11.         int[][] twoD = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
  12.         ArrayList<Integer> aList = new ArrayList<Integer>();
  13.         aList.addAll(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
  14.         isSorted(sorted);
  15.         for (int i = 0; i < flatten(twoD).length; i++) {
  16.             System.out.print(flatten(twoD)[i]);
  17.         }
  18.         System.out.print("\n");
  19.         flatten(twoD);
  20.         arrayCompare(sorted, aList);
  21.  
  22.     }
  23.  
  24.     public static void isSorted(int[] array) {
  25.         boolean sorted = false;
  26.         for (int i = 0; i < array.length - 1; i++) {
  27.             if (array[i] < array[i + 1]) {
  28.                 sorted = true;
  29.             } else {
  30.                 sorted = false;
  31.             }
  32.         }
  33.         if (sorted) {
  34.             System.out.println("The array is sorted from Smallest to Largest");
  35.         } else {
  36.             System.out.println("The array is not sorted");
  37.         }
  38.  
  39.     }
  40.  
  41.     public static int[] flatten(int[][] array) {
  42.         int arrayLength = array[0].length * array[1].length;
  43.         int[] flat = new int[arrayLength];
  44.         int count = 0;
  45.         for (int i = 0; i < array.length; i++) {
  46.             for (int j = 0; j < array[i].length; j++) {
  47.                 flat[count] = array[i][j];
  48.                 count++;
  49.  
  50.             }
  51.         }
  52.         return flat;
  53.  
  54.     }
  55.  
  56.     public static void arrayCompare(int[] array, ArrayList<Integer> arrayList) {
  57.         boolean same = false;
  58.         for (int i = 0; i < array.length; i++) {
  59.             if (array[i] == arrayList.get(i)) {
  60.                 same = true;
  61.             }
  62.         }
  63.         if (same) {
  64.             System.out.println("the array and the ArrayList have the same set of elements in the same order!");
  65.         } else {
  66.             System.out.println("the array and the ArrayList dont match");
  67.         }
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement