Freaking arrays
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.calebfontenot</groupId>
|
||||
<artifactId>lab-2D-arrays-sort_CalebFontenot</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<exec.mainClass>com.calebfontenot.lab.d.arrays.sort_calebfontenot.Lab2DArraysSort_CalebFontenot</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
package com.calebfontenot.lab.d.arrays.sort_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Lab2DArraysSort_CalebFontenot {
|
||||
|
||||
public static int maxNumberOfColumnsInJagged2dArray(char[][] 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 printColumnMajorOrder(char[][] 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("");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints row by row
|
||||
*
|
||||
* @param ar
|
||||
*/
|
||||
public static void printRowMajorOrder(char[][] ar)//normal
|
||||
{
|
||||
for (int x = 0; x < ar.length; ++x) {
|
||||
for (int y = 0; y < ar[x].length; ++y) {
|
||||
System.out.print(ar[x][y] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the methods in ascending order using Selection Sort
|
||||
*
|
||||
* @param names the names to be sorted
|
||||
*/
|
||||
public static void sortNames(char[][] names) {
|
||||
for (int arrayDim = 0; arrayDim < names[0].length; ++arrayDim) {
|
||||
for (int i = 0; i < names[arrayDim].length - 1; ++i) {
|
||||
for (int j = i + 1; j < names[arrayDim].length; ++j) {
|
||||
// Compare all chars in string if first chars match
|
||||
char compChar1 = names[arrayDim][i].charAt(0), compChar2 = names[arrayDim][j].charAt(0);
|
||||
if (Character.toLowerCase(compChar1) == Character.toLowerCase(compChar2)) {
|
||||
compChar1 = names[arrayDim][i].charAt(1);
|
||||
compChar2 = names[arrayDim][j].charAt(1);
|
||||
|
||||
// Note: I'm not happy with this implementation, but it works as long as you're only having to compare 1 other character.
|
||||
}
|
||||
if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
|
||||
String temp = names[i];
|
||||
names[i] = names[j];
|
||||
names[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
char[][] names = {
|
||||
{'j', 'o', 'h', 'n'},
|
||||
{'a', 'n'},
|
||||
{'b', 'y', 'r', 'o', 'n'},};
|
||||
System.out.println(maxNumberOfColumnsInJagged2dArray(names));
|
||||
//printColumnMajorOrder(names);
|
||||
printRowMajorOrder(names);
|
||||
sortNames(names);
|
||||
System.out.println();
|
||||
printRowMajorOrder(names);
|
||||
//printColumnMajorOrder(names);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user