Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package arrayQuiz1;
- import java.util.ArrayList;
- import java.util.Arrays;
- public class Quiz1 {
- public static void main(String[] args) {
- int[] sorted = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- int[] unSorted = { 3, 5, 6, 7, 8, 9, 2, 1 };
- int[][] twoD = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
- ArrayList<Integer> aList = new ArrayList<Integer>();
- aList.addAll(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
- isSorted(sorted);
- for (int i = 0; i < flatten(twoD).length; i++) {
- System.out.print(flatten(twoD)[i]);
- }
- System.out.print("\n");
- flatten(twoD);
- arrayCompare(sorted, aList);
- }
- public static void isSorted(int[] array) {
- boolean sorted = false;
- for (int i = 0; i < array.length - 1; i++) {
- if (array[i] < array[i + 1]) {
- sorted = true;
- } else {
- sorted = false;
- }
- }
- if (sorted) {
- System.out.println("The array is sorted from Smallest to Largest");
- } else {
- System.out.println("The array is not sorted");
- }
- }
- public static int[] flatten(int[][] array) {
- int arrayLength = array[0].length * array[1].length;
- int[] flat = new int[arrayLength];
- int count = 0;
- for (int i = 0; i < array.length; i++) {
- for (int j = 0; j < array[i].length; j++) {
- flat[count] = array[i][j];
- count++;
- }
- }
- return flat;
- }
- public static void arrayCompare(int[] array, ArrayList<Integer> arrayList) {
- boolean same = false;
- for (int i = 0; i < array.length; i++) {
- if (array[i] == arrayList.get(i)) {
- same = true;
- }
- }
- if (same) {
- System.out.println("the array and the ArrayList have the same set of elements in the same order!");
- } else {
- System.out.println("the array and the ArrayList dont match");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement