“Arris avec pour boucles” Réponses codées

Arris avec pour boucles

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;
}
Encouraging Echidna

Arris avec pour boucles

String[] words = new String[] { "hello", "foo", "bar" };
int[] squares = new int[] { 1, 4, 9, 16 };
Encouraging Echidna

Arris avec pour boucles

// 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] );
  }
}
Encouraging Echidna

Arris avec pour boucles

// Another way to write search that combines
// the "end of array" and "found" logic in one
// while loop. As a matter of style, we prefer
// the above version that uses the standard
// for-all loop.
public int searchNotAsGood(int[] nums, int target) {
  int i = 0;
  while (i<nums.length && nums[i]!=target) {
    i++;
  }
  // get here either because we found it, or hit end of array
  if (i==nums.length) {
    return -1;
  }
  else {
    return i;
  }
}
Encouraging Echidna

Arris avec pour boucles

// 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;
}
Encouraging Echidna

Arris avec pour boucles

// 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;
}
Encouraging Echidna

Arris avec pour boucles

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);
Encouraging Echidna

Arris avec pour boucles

// 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] );
  }
}
Encouraging Echidna

Arris avec pour boucles

// 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;
}
Encouraging Echidna

Arris avec pour boucles

// 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;
}
Encouraging Echidna

Réponses similaires à “Arris avec pour boucles”

Questions similaires à “Arris avec pour boucles”

Plus de réponses similaires à “Arris avec pour boucles” dans Java

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code