Adelin
0
Q:

arrays with for loops

int findSmallest(int[] values) {
  int minIndex = 0;  // start with 0th element as min
  for (int i=1; i<values.length; i++) {
    if (values[i] < values[minIndex]) {
      minIndex = i;
    }
  }
  return minIndex;
}
0
// Find-Max variant -- rather than finding the max value, find the
// *index* of the max value
public int findMaxIndex(int[] nums) {
  int maxIndex = 0;  // say the 0th element is the biggest
  int maxValue = nums[0];

  // Look at every element, starting at 1
  for (int i=1; i<nums.length; i++) {
    if (nums[i] > maxValue) {
      maxIndex = i;
      maxValue = nums[maxIndex];
    }
  }
  return maxIndex;
}
0
// Search
// Searches through the given array looking
// for the given target. Returns the index number
// of the target if found, or -1 otherwise.
// (classic search-loop example)
public int search(int[] nums, int target) {
  // Look at every element
  for (int i=0; i<nums.length; i++) {
    if (nums[i] == target) {
      return i; // return the index where the target is found
    }
  }

  // If we get here, the target was not in the array
  return -1;
}
0
// For-All variant
// Go through the elements backwards
// by adjusting the for-loop
public void forAllBackwards(int[] nums) {
  for (int i = nums.length-1; i>=0; i--) {
    System.out.println( nums[i] );
  }
}
0
// Find-Max variant
// Use very small number, Integer.MIN_VALUE,
// as the initial max value.
public int findMax2(int[] nums) {
  int maxSoFar = Integer.MIN_VALUE;  // about -2 billion

  // Look at every element
  for (int i=0; i<nums.length; i++) {
    if (nums[i] > maxSoFar) {
      maxSoFar = nums[i];
    }
  }
  
  return maxSoFar;
}
0
// For-All
// Do something for every element
public void forAll(int[] nums) {
  for (int i=0; i<nums.length; i++) {
    System.out.println( nums[i] );
  }
}
0
// Find-Max
// Given a non-empty array of ints, returns
// the largest int value found in the array.
// (does not work with empty arrays)
public int findMax(int[] nums) {
  int maxSoFar = nums[0];  // use nums[0] as the max to start

  // Look at every element, starting at 1
  for (int i=1; i<nums.length; i++) {
    if (nums[i] > maxSoFar) {
      maxSoFar = nums[i];
    }
  }
  return maxSoFar;
}
0
String[] words = new String[] { "hello", "foo", "bar" };
int[] squares = new int[] { 1, 4, 9, 16 };
0
// Given an array of booleans representing a series
// of coin tosses (true=heads, false=tails),
// returns true if the array contains anywhere within it
// a string of 10 heads in a row.
// (example of a search loop)
public boolean searchHeads(boolean[] heads) {
  int streak = 0;     // count the streak of heads in a row
  
  for (int i=0; i<heads.length; i++) {
    if (heads[i]) {   // heads : increment streak
      streak++;
      if (streak == 10) {
        return true;  // found it!
      }
    }
    else {            // tails : streak is broken
      streak = 0;
    }
  }
  
  // If we get here, there was no streak of 10
  return false;
}
0
Account[] accounts;

// 1. allocate the array (initially all the pointers are null)
accounts = new Account[100];	

// 2. allocate 100 Account objects, and store their pointers in the array
for (int i=0; i<accounts.length; i++) {
  accounts[i] = new Account();
}

// 3. call a method on one of the accounts in the array
account[0].deposit(100);
0

Tags

New to Communities?

Join the community