Unit 6 - Arrays
public class ArrayMethods
{
private static int[] values;
public ArrayMethods (int[] initialValues)
{
values = initialValues;
}
/*
* Swaps the first and last element in the array
*/
public void swapFirstAndLast ()
{
int size = values.length;
int temp = values[size-1];
values[size-1] = values[0];
values[0] = temp;
}
/*
* Prints the arrays
*/
public void printArray ()
{
for (int i = 0 ; i < values.length ; i++)
{
System.out.printf("%d ", values[i]);
}
System.out.printf("\n");
}
public static void main (String[] Args)
{
int[] array = {1, 4, 9, 16, 25};
ArrayMethods arrayMethods = new ArrayMethods(array);
System.out.printf("Original array:\n");
arrayMethods.printArray();
arrayMethods.swapFirstAndLast();
System.out.printf("First and last elements switched:\n");
arrayMethods.printArray();
}
}
ArrayMethods.main(null);
public class ArrayMethods
{
private static int[] values;
public ArrayMethods (int[] initialValues)
{
values = initialValues;
}
/*
* Replaces every even element with 0
*/
public void replaceEvenWith0 ()
{
for (int i = 0 ; i < values.length ; i++)
{
if (i % 2 == 0)
{
values[i] = 0;
}
}
}
/*
* Prints the arrays
*/
public void printArray ()
{
for (int i = 0 ; i < values.length ; i++)
{
System.out.printf("%d ", values[i]);
}
System.out.printf("\n");
}
public static void main (String[] Args)
{
int[] array = {1, 4, 9, 16, 25};
ArrayMethods arrayMethods = new ArrayMethods(array);
System.out.printf("Original array:\n");
arrayMethods.printArray();
System.out.printf("Every even element replaced with 0:\n");
arrayMethods.replaceEvenWith0();
arrayMethods.printArray();
}
}
ArrayMethods.main(null);
public class ArrayMethods
{
private static int[] values;
public ArrayMethods (int[] initialValues)
{
values = initialValues;
}
/*
* Returns true if the array is sorted in increasing order
*/
public boolean sortIncreasingOrder()
{
for (int i = 0 ; i < values.length - 1 ; i++)
{
if (values[i] > values[i+1] )
{
return false;
}
}
return true;
}
/*
* Prints the arrays
*/
public void printArray ()
{
for (int i = 0 ; i < values.length ; i++)
{
System.out.printf("%d ", values[i]);
}
System.out.printf("\n");
}
public static void main (String[] Args)
{
int[] array = {1, 4, 9, 16, 25};
ArrayMethods arrayMethods = new ArrayMethods(array);
System.out.printf("Sorted Order: %b\n", arrayMethods.sortIncreasingOrder());
arrayMethods.printArray();
}
}
ArrayMethods.main(null);
public class ArrayMethods
{
private static int[] values;
public ArrayMethods (int[] initialValues)
{
values = initialValues;
}
/*
* Returns true if the array has any duplicate elements
*/
public boolean duplicates ()
{
Arrays.sort(values);
for (int i = 0 ; i < values.length-1 ; i++)
{
if (values[i] == values[i+1])
{
return true;
}
}
return false;
}
/*
* Prints the arrays
*/
public void printArray ()
{
for (int i = 0 ; i < values.length ; i++)
{
System.out.printf("%d ", values[i]);
}
System.out.printf("\n");
}
public static void main (String[] Args)
{
int[] array = {43, 13, 8, 3, 5, 21, 1, 0, 1, 2};
ArrayMethods arrayMethods = new ArrayMethods(array);
System.out.printf("Before sort: ");
arrayMethods.printArray();
boolean duplicates = arrayMethods.duplicates();
System.out.printf("After sort: ");
arrayMethods.printArray();
System.out.printf("Adjacent duplicate elements: %b\n", duplicates);
}
}
ArrayMethods.main(null);