Unit 06 Tue - Arrays
Homework
import java.util.*;
import java.io.*;
public class ArrayMethods {
private int[] values;
public ArrayMethods(int[] array) {
this.values = array;
}
public int[] getArray() {
return this.values;
}
public String getString() {
return Arrays.toString(this.values);
}
public void swap() {
int temp = values[0];
values[0] = values[values.length - 1];
values[values.length - 1] = temp;
}
public void replaceAll() {
for (int i = 0; i < values.length; i++) {
values[i] = 0;
}
}
public boolean increasingOrder() {
for (int i = 0; i < this.values.length - 1; i++) {
if (values[i] > values[i+1]) {
return false;
}
return true;
}
return false;
}
// Tester Method
public static void main() {
ArrayMethods method = new ArrayMethods(new int[]{1,2,3,3,4,5,5});
System.out.println("~~~ Array ~~~");
System.out.println(method.getString());
System.out.println("~~~ Increasing Order ~~~");
System.out.println(method.increasingOrder());
System.out.println("~~~ Swap First and Last ~~~");
method.swap();
System.out.println(method.getString());
System.out.println("~~~ Replace All W/ 0 ~~~");
method.replaceAll();
System.out.println(method.getString());
}
}
ArrayMethods.main();
public class Array {
public static void main(String[] args)
{
int[] array = {1, 2, 3, 4, 5, 4, 3};
System.out.println("~~~ Duplicates ~~~");
System.out.println(Arrays.toString(array));
for (int i = 0; i < array.length-1; i++)
{
for (int j = i+1; j < array.length; j++)
{
if ((array[i] == array[j]) && (i != j))
{
System.out.println(array[j]);
}
}
}
}
}
Array.main(null);