lab 16
This commit is contained in:
parent
9935979398
commit
4fef370132
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* 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.calebfontenot.lab16_arrays2_calebfontenot;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author caleb
|
||||||
|
*/
|
||||||
|
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] + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* 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.calebfontenot.lab16_arrays2_calebfontenot;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author caleb
|
||||||
|
*/
|
||||||
|
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] + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* 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.calebfontenot.lab16_arrays2_calebfontenot;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author caleb
|
||||||
|
*/
|
||||||
|
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] + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* 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.calebfontenot.lab16_arrays2_calebfontenot;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author caleb
|
||||||
|
*/
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
@ -9,18 +9,23 @@ package com.calebfontenot.lab16_arrays2_calebfontenot;
|
|||||||
* @author caleb
|
* @author caleb
|
||||||
*/
|
*/
|
||||||
public class ArrayShift1 {
|
public class ArrayShift1 {
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
int[] ar = {1, 2, 3, 4, 5};
|
int[] ar = {1, 2, 3, 4, 5};
|
||||||
int temp = ar[0];
|
int temp = ar[0];
|
||||||
for (int i = 1; i < ar.length; ++i) {
|
for (int value : ar) {
|
||||||
ar[i-1] = ar[i];
|
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;
|
ar[ar.length - 1] = temp;
|
||||||
|
|
||||||
for (int value: ar) {
|
for (int value : ar) {
|
||||||
System.out.println(value + " ");
|
System.out.print(value + " ");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* 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.calebfontenot.lab16_arrays2_calebfontenot;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author caleb
|
||||||
|
*/
|
||||||
|
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 + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,81 @@
|
|||||||
|
/*
|
||||||
|
* 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.calebfontenot.lab16_arrays2_calebfontenot;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author caleb
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -2,9 +2,6 @@
|
|||||||
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
|
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
|
||||||
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
|
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
|
||||||
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
|
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
|
||||||
<group>
|
<group/>
|
||||||
<file>file:/home/caleb/ASDV-Java/Exams/Exam2_CalebFontenot/src/exam2_calebfontenot/Problem3.java</file>
|
|
||||||
<file>file:/home/caleb/ASDV-Java/Exams/Exam2_CalebFontenot/src/exam2_calebfontenot/Problem1.java</file>
|
|
||||||
</group>
|
|
||||||
</open-files>
|
</open-files>
|
||||||
</project-private>
|
</project-private>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user