Reset author name to chosen name ✨
This commit is contained in:
@@ -1,209 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.lab.d.arrays_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Lab2DArrays_ChloeFontenot {
|
||||
|
||||
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} }, // table 1 NOT jagged 2x2
|
||||
{{5, 6}, {-1} }, // table 2 jagged
|
||||
{{7, 8 ,9}, {10, 11}, {12, 13, 14, 15}} // table 3 jagged
|
||||
};
|
||||
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) {
|
||||
// Compare all chars in string if first chars match
|
||||
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) {
|
||||
// If it's failed, the index has gone out of range.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
|
||||
String temp = ar1[i];
|
||||
ar1[i] = ar1[j];
|
||||
ar1[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user