Unit 6 Array Hacks
public class FindEven{
public static void main(String args[]){
int arrayOne[]= {1, 3, 5, 7, 8}; //changed 9 to 8
for (int i = 0; i < arrayOne.length; i++) {
if (arrayOne[i]%2 == 0){
System.out.println(arrayOne[i]);
}
}
}
}
FindEven.main(null);
public class Ascend{
public static void main(String args[]){
int[] myNumbers = new int[] {5, 3, 4, 1, 2};
Arrays.sort(myNumbers); //use sort() method of the Arrays class
for (int i = 0; i < myNumbers.length; i++) {
System.out.println(myNumbers[i]);
}
}
}
Ascend.main(null);
public class Ascend2{
public static void main(String args[]){
int[] myNumbers = new int[] {5, 3, 4, 1, 2};
for (int i = 0; i < myNumbers.length; i++) { //using for loops and logics
for (int j = i + 1; j < myNumbers.length; j++) {
int tmp = 0;
if (myNumbers[i] > myNumbers[j]) {
tmp = myNumbers[i];
myNumbers[i] = myNumbers[j];
myNumbers[j] = tmp;
}
}
System.out.println(myNumbers[i]);
}
}
}
Ascend2.main(null);
Hack 4 Given the following code segment, which of the following will cause an infinite loop? Assume that temp is an int variable initialized to be greater than zero and that a is an array of integers.
for ( int k = 0; k < a.length; k++ ) { while ( a[ k ] < temp ) { a[ k ] *= 2; } }
B
When a contains a value that is less than or equal to zero then multiplying that value by 2 will never make the result larger than temp (which was set to some value > 0), so an infinite loop will occur.
6.3 Enhanced for loop for Arrays
- can only traverse in a forward direction
for (dataType i: arrayName) {
do something with i
}
public class ForEachDemo
{
public static void main(String[] args)
{
int[] highScores = { 10, 9, 8, 8};
String[] names = {"Jamal", "Emily", "Destiny", "Mateo"};
// for each loop with an int array
for (int value : highScores)
{
System.out.println( value );
}
// for each loop with a String array
for (String value : names)
{
System.out.println(value); // this time it's a name!
}
}
}
Attempted to add element to array in the above code failed. Below, attempting to add element to an int array by creating a new array
- Create a new array of size n+1, where n is the size of the original array.
- Add the n elements of the original array in this array.
- Add the new element in the n+1 th position.
- Print the new array.
import java.io.*;
import java.lang.*;
import java.util.*;
public class AddElement {
// Function to add x in arr
public static int[] addX(int n, int highScores[], int x)
{
// create a new array of size n+1
int newarr[] = new int[n + 1];
// insert the elements from
// the old array into the new array
// insert all elements till n
// then insert x at n+1
for (int i = 0; i < n; i++)
newarr[i] = highScores[i];
newarr[n] = x;
return newarr;
}
// Driver code
public static void main(String[] args)
{
int n = 4;
// initial array of size 10
int[] highScores = {10, 9, 8, 8};
// print the original array
System.out.println(Arrays.toString(highScores));
// element to be added
int x = 50;
// call the method to add x in arr
highScores = addX(n, highScores, x);
// print the updated array
System.out.println(Arrays.toString(highScores));
}
}
AddElement.main(null);
Figured out that I misunderstood the hack. It's just asking to see if it can run. This is the runtime after directly adding new values to arrays.
public class ForEachDemo
{
public static void main(String[] args)
{
int[] highScores = { 10, 9, 8, 8, 5};
String[] names = {"Jamal", "Emily", "Destiny", "Mateo", "Linda"};
// for each loop with an int array
for (int value : highScores)
{
System.out.println( value );
}
// for each loop with a String array
for (String value : names)
{
System.out.println(value); // this time it's a name!
}
}
}
ForEachDemo.main(null);
B
class RotateLeft {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {7, 9, 4};
//n determine the number of times an array should be rotated
int n = 1;
//Displays original array
System.out.println("Original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
//Rotate the given array by n times toward left
for(int i = 0; i < n; i++){
int j, first;
//Stores the first element of the array
first = arr[0];
for(j = 0; j < arr.length-1; j++){
//Shift element of array by one
arr[j] = arr[j+1];
}
//First element of array will be added to the end
arr[j] = first;
}
System.out.println();
//Displays resulting array after rotation
System.out.println("Array after left rotation: ");
for(int i = 0; i< arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
RotateLeft.main(null);
public class Duplicate {
public static void main(String[] args) {
int[] values = {1,2,1,3,1,4};
duplicate(values);
}
public static void duplicate(int numbers[]) {
Arrays.sort(numbers); //first sort
int previous = numbers[0] - 1;
int dupCount = 0; //counter
for (int i = 0; i < numbers.length; ++i) {
if (numbers[i] == previous) { //if equal, counter + 1
++dupCount;
} else {
previous = numbers[i];
}
}
System.out.println("Numbers of duplicate elements: " + dupCount);
}
}
Duplicate.main(null);
public class Reverse
{
public static void main(String[] args) {
String[] Array = {"h", "e", "l", "l", "o"};
//print array starting from first element
System.out.println("Original Array:");
for(int i=0;i<Array.length;i++)
System.out.print(Array[i] + " ");
System.out.println();
//print array starting from last element
System.out.println("Reverse order:");
for(int i=Array.length-1;i>=0;i--)
System.out.print(Array[i] + " ");
}
}
Reverse.main(null);
Write the ClubMembers method addMembers, which takes two parameters. The first parameter is a String array containing the names of new club members to be added. The second parameter is the graduation year of all the new club members. The method adds the new members to the memberList instance variable. The names can be added in any order. All members added are initially in good standing and share the same graduation year, gradYear.
Complete the addMembers method.
public void addMembers(String[] names, int gradYear)
{
for (int i =0; i < addMembers.length; i++){ . //for loop to loops through all members
memberList.add(new MemberInfo(name, gradYear, true)); //add member to memberList
}
}