Reset author name to chosen name ✨
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.lab16_arrays2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class ArrayCopy1 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int[] sourceArray = {1, 2, 3, 4, 5};
|
||||
int[] targetArray = new int[sourceArray.length];
|
||||
// Print the source array
|
||||
for (int i = 0; i < sourceArray.length; i++) {
|
||||
System.out.print(sourceArray[i] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
// Copy the source array to the target array
|
||||
for (int i = 0; i < sourceArray.length; i++) {
|
||||
targetArray[i] = sourceArray[i];
|
||||
}
|
||||
// Print the target array
|
||||
for (int i = 0; i < sourceArray.length; i++) {
|
||||
System.out.print(targetArray[i] + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.lab16_arrays2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class ArrayCopy2 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int[] sourceArray = {1, 2, 3, 4, 5};
|
||||
int[] targetArray = new int[sourceArray.length];
|
||||
// Print the source array
|
||||
for (int i = 0; i < sourceArray.length; i++) {
|
||||
System.out.print(sourceArray[i] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
// Copy the source array to the target array in reversed order
|
||||
int j = (sourceArray.length - 1);
|
||||
for (int i = 0; i < sourceArray.length; i++) {
|
||||
targetArray[i] = sourceArray[j];
|
||||
j--;
|
||||
}
|
||||
// Print the target array
|
||||
for (int i = 0; i < sourceArray.length; i++) {
|
||||
System.out.print(targetArray[i] + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.lab16_arrays2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class ArrayCopy3 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int[] sourceArray = {1, 2, 3, 4, 5};
|
||||
int[] targetArray = new int[sourceArray.length];
|
||||
// Print the source array
|
||||
for (int i = 0; i < sourceArray.length; i++) {
|
||||
System.out.print(sourceArray[i] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
// Copy the source array to the target array
|
||||
System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
|
||||
// Print the target array
|
||||
for (int i = 0; i < sourceArray.length; i++) {
|
||||
System.out.print(targetArray[i] + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.lab16_arrays2_chloefontenot;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class ArrayCopy4 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int[] array = {1, 2, 3, 4, 5};
|
||||
int[] array1 = Arrays.copyOfRange(array, 2, 5);
|
||||
int[] array2 = new int[array.length - 2];
|
||||
int[] array3 = new int[array.length];
|
||||
System.out.println("array1: (copy with java.util.Arrays.copyOfRange(); )");
|
||||
for (int i: array1) {
|
||||
System.out.print(i + " ");
|
||||
}
|
||||
System.out.println();
|
||||
// Copy array into array2 with a for loop and print it
|
||||
System.out.println("array2: (copy with System.arraycopy(); )");
|
||||
System.arraycopy(array, 2, array2, 0, 3);
|
||||
for (int i : array2) {
|
||||
System.out.print(i + " ");
|
||||
}
|
||||
System.out.println();
|
||||
// Copy array manually
|
||||
System.out.println("array3: (manually copy with for loop)");
|
||||
for (int i : array3) {
|
||||
array[i] = array3[i];
|
||||
}
|
||||
for (int i : array1) {
|
||||
System.out.print(i + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.lab16_arrays2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class ArrayShift1 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int[] ar = {1, 2, 3, 4, 5};
|
||||
int temp = ar[0];
|
||||
for (int value : ar) {
|
||||
System.out.print(value + " ");
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
for (int i = 1; i < ar.length; ++i) {
|
||||
ar[i - 1] = ar[i];
|
||||
}
|
||||
ar[ar.length - 1] = temp;
|
||||
|
||||
for (int value : ar) {
|
||||
System.out.print(value + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.lab16_arrays2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class ArrayShift2 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int[] ar = {1, 2, 3, 4, 5};
|
||||
int temp = ar[ar.length - 1];
|
||||
|
||||
for (int value : ar) {
|
||||
System.out.print(value + " ");
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
for (int i = (ar.length -1); i > 0 ; --i) {
|
||||
ar[i] = ar[i -1];
|
||||
}
|
||||
ar[0] = temp;
|
||||
|
||||
for (int value : ar) {
|
||||
System.out.print(value + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.lab16_arrays2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
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};
|
||||
// Use the built-in sort method to sort the double.
|
||||
java.util.Arrays.sort(doubles);
|
||||
|
||||
char[] chars = {'a', 'A', '4', 'F', 'D', 'P',};
|
||||
//Sort again
|
||||
java.util.Arrays.sort(chars);
|
||||
// Now print the sorted arrays.
|
||||
printArray(doubles);
|
||||
printArray(chars);
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.lab16_arrays2_chloefontenot;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class ArraysAndMethodsGames {
|
||||
public static double min(double[] array) {
|
||||
// pull the first number from the array, and compare that with the numbers in the rest of the array. If that number is less than the number in than the first number, set it to our lowest.
|
||||
double lowest = array[0];
|
||||
for (int i = 1; i < (array.length -1); i++) {
|
||||
lowest = Math.min(lowest, array[i]);
|
||||
}
|
||||
return lowest;
|
||||
}
|
||||
|
||||
public static double[] readArray() {
|
||||
// Create a double array.
|
||||
int doublesRemaining = 10;
|
||||
double[] returnArray = new double[doublesRemaining];
|
||||
// Create Scanner
|
||||
Scanner input = new Scanner(System.in);
|
||||
// Read characters input and write them to an array.
|
||||
int i = 0;
|
||||
do {
|
||||
System.out.print("Enter 10 doubles (" + doublesRemaining + " left ): ");
|
||||
returnArray[i] = input.nextDouble();
|
||||
i++;
|
||||
doublesRemaining--;
|
||||
} while (doublesRemaining != 0);
|
||||
return returnArray;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array
|
||||
* @return Returns an average of an array.
|
||||
*/
|
||||
public static double average(double[] array) {
|
||||
// Iterate through array and add values together into double, then divide by the amount of items in the array.
|
||||
double returnDouble = 0.0;
|
||||
for (double i: array) {
|
||||
returnDouble += i;
|
||||
}
|
||||
returnDouble /= (double) array.length;
|
||||
return returnDouble;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
double[] doubleArray = new double[10];
|
||||
doubleArray = readArray();
|
||||
System.out.println("average function returns: " + average(doubleArray));
|
||||
System.out.println("lowest function returns: " + min(doubleArray));
|
||||
}
|
||||
}
|
||||
@@ -1,17 +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.lab16_arrays2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Lab16_arrays2_ChloeFontenot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user