/home/caleb/ASDV-Java/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays_calebfontenot/Lab2DArrays_CalebFontenot.java |
package com.calebfontenot.lab.d.arrays_calebfontenot;
public class Lab2DArrays_CalebFontenot {
public static void main(String[] args)
{
int[][] a1 = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
String[][] a2 = {
{"john", "paul", "james"},
{"mary", "laura", "margaret"}
};
print(a1);
System.out.println();
print(a2);
int a3[][] = new int[5][5];
initializeArray(a3);
System.out.println();
print(a3);
System.out.println();
int[][] dupDup = dup(a1);
print(dupDup);
System.out.println();
String[][] a4 = {
{"ASDV", "MATH", "ENGL"},
{"BIOL", "CHEM"},
{"PHYS"}
};
print(a4);
System.out.println();
printColumnMajorOrder(a4);
System.out.println();
int[][] a5 = {
{1},
{1, 2},
{1, 2, 3}
};
System.out.println("Max number of columns in ar5 is " + maxNumberOfColumnsInJagged2DArray(a5));
System.out.println();
int[][][] a6 = {
{ {1, 2}, {3, 4} },
{{5, 6}, {-1} },
{{7, 8 ,9}, {10, 11}, {12, 13, 14, 15}}
};
print(a6);
String[] a7 = {"john", "Mary", "Paul", "nick", "Peter", "anna"};
System.out.println();
selectionSort(a7);
print(a7);
}
public static void print(int[][] a1)
{
for (int i = 0; i < a1.length; ++i) {
for (int j = 0; j < a1[i].length; ++j) {
System.out.print(a1[i][j] + " ");
}
System.out.println();
}
}
public static void print(int[][][] a1)
{
for (int i = 0; i < a1.length; ++i) {
System.out.println("\n Table: " + (i+1));
for (int j = 0; j < a1[i].length; ++j) {
for (int k = 0; k < a1[i][j].length; ++k) {
System.out.print(a1[i][j][k] + " ");
}
System.out.println();
}
}
}
public static void print(String[][] a1)
{
for (int i = 0; i < a1.length; ++i) {
for (int j = 0; j < a1[i].length; ++j) {
System.out.print(a1[i][j] + " ");
}
System.out.println();
}
}
public static void print(String[] a1)
{
for (int i = 0; i < a1.length; ++i) {
System.out.print(a1[i] + " ");
}
System.out.println();
}
public static void initializeArray(int[][] a1)
{
for (int i = 0; i < a1.length; ++i) {
for (int j = 0; j < a1[i].length; ++j) {
a1[i][j] = (int) (Math.random() * 11);
}
}
}
public static int[][] dup(int[][] a1)
{
int[][] dupArray = new int[a1.length][a1[0].length];
for (int i = 0; i < a1.length; ++i) {
for (int j = 0; j < a1[i].length; ++j) {
dupArray[i][j] = a1[i][j];
}
}
return dupArray;
}
public static void printColumnMajorOrder(String[][] ar)
{
int row = 0;
int column = 0;
int max = maxNumberOfColumnsInJagged2DArray(ar);
for (column = 0; column < max; ++column) {
for (row = 0; row < ar.length; ++row) {
if (column < ar[row].length) {
System.out.print(ar[row][column] + " ");
}
}
System.out.println();
}
}
public static int maxNumberOfColumnsInJagged2DArray(int[][] ar) {
int maxNumberOfColumns = 0;
for (int row = 0; row < ar.length; ++row) {
if (ar[row].length > maxNumberOfColumns) {
maxNumberOfColumns = ar[row].length;
}
}
return maxNumberOfColumns;
}
public static int maxNumberOfColumnsInJagged2DArray(String[][] ar) {
int maxNumberOfColumns = 0;
for (int row = 0; row < ar.length; ++row) {
if (ar[row].length > maxNumberOfColumns) {
maxNumberOfColumns = ar[row].length;
}
}
return maxNumberOfColumns;
}
public static void printARow(int[] rowOf2D) {
for (int i = 0; i < rowOf2D.length; ++i) {
System.out.print(rowOf2D[i] + " ");
}
System.out.println();
}
public static void selectionSort(int[] ar1) {
for (int i = 0; i < ar1.length - 1; ++i) {
for (int j = i + 1; j < ar1.length; ++j) {
if (ar1[i] > ar1[j]) {
int temp = ar1[i];
ar1[i] = ar1[j];
ar1[j] = temp;
}
}
}
}
public static void selectionSort(String[] ar1) {
for (int i = 0; i < ar1.length - 1; ++i) {
for (int j = i + 1; j < ar1.length; ++j) {
char compChar1 = ar1[i].charAt(0), compChar2 = ar1[j].charAt(0);
for (int index = 1; index < ar1[i].length(); ++index) {
if (Character.toLowerCase(compChar1) == Character.toLowerCase(compChar2)) {
try {
compChar1 = ar1[i].charAt(index);
compChar2 = ar1[j].charAt(index);
} catch (Exception ex) {
break;
}
}
}
if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
String temp = ar1[i];
ar1[i] = ar1[j];
ar1[j] = temp;
}
}
}
}
}