/home/caleb/ASDV-Java/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArraysAndMethods.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package com.calebfontenot.lab16_arrays2_calebfontenot;
@author
public class ArraysAndMethods {
public static void printArray(int[] arrayInput)
{
for (int i : arrayInput) {
System.out.print(i + " ");
}
System.out.println();
}
public static void printArray(char[] arrayInput)
{
for (int i : arrayInput) {
System.out.print((char) i + " ");
}
System.out.println();
}
public static void printArray(double[] arrayInput)
{
for (double i : arrayInput) {
System.out.print(i + " ");
}
System.out.println();
}
public static int[] reverse(int[] arrayToReverse)
{
int arrayResult[] = new int[arrayToReverse.length];
int j = (arrayToReverse.length - 1);
for (int i = 0; i <= arrayToReverse.length - 1; i++) {
arrayResult[i] = arrayToReverse[j];
j--;
}
return arrayResult;
}
public static int linearSearch(int list[], int key)
{
int indexFound = -1;
for (int i = 0; i < list.length; ++i) {
if (list[i] == key) {
indexFound = i;
}
}
return indexFound;
}
public static void main(String[] args)
{
int[] array = {1, 2, 3, 4, 5};
printArray(array);
printArray(new int[]{3, 1, 2, 6, 4});
printArray(reverse(array));
System.out.println(linearSearch(array, 5) != 1
? "found at index " + Integer.toString(linearSearch(array, 5))
: "not found at index " + Integer.toString(linearSearch(array, 5))
);
double[] doubles = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5};
java.util.Arrays.sort(doubles);
char[] chars = {'a', 'A', '4', 'F', 'D', 'P',};
java.util.Arrays.sort(chars);
printArray(doubles);
printArray(chars);
}
}