Commit 47466fbd authored by Trisha Gee's avatar Trisha Gee
Browse files

Failing for loop examples

parent 4b3a87c5
Loading
Loading
Loading
Loading
+1 −1
Changes for .idea/gradle.xml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@
  <component name="GradleSettings">
    <option name="linkedExternalProjectsSettings">
      <GradleProjectSettings>
        <option name="delegatedBuild" value="true" />
        <option name="delegatedBuild" value="false" />
        <option name="testRunner" value="PLATFORM" />
        <option name="distributionType" value="DEFAULT_WRAPPED" />
        <option name="externalProjectPath" value="$PROJECT_DIR$" />
+56 −0
Changes for src/main/java/Ch10c.java: 56 added lines, 0 removed lines.
Original line number Diff line number Diff line
import java.util.Arrays;
import java.util.List;

public class Ch10c {
@@ -8,8 +9,63 @@ public class Ch10c {
            System.out.println(color);
        }
    }

    void forEachExample() {
        List<String> allColors = List.of("Red", "Blue", "Yellow");
        allColors.forEach(color -> System.out.println(color));
    }

    public static void main(String[] args) {
        badFor4();
    }

    // 2, 3, 4, 5
    static void badFor1() {
        int[] ints = {1, 2, 3, 4, 5};
        for (int i = 1; i < ints.length; i++) {
            System.out.println(ints[i]);
        }
    }

    //Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
    //	at Ch10c.badFor2(Ch10c.java:31)
    //	at Ch10c.main(Ch10c.java:18)
    static void badFor2() {
        int[] ints = {1, 2, 3, 4, 5};
        for (int i = 0; i <= ints.length; i++) {
            System.out.println(ints[i]);
        }
    }

    // Doesn't compile
    static void badFor3() {
//        int[] ints = {1, 2, 3, 4, 5};
//        for (int i = 0; i < ints.size(); i++) {
//            System.out.println(ints[i]);
//        }
    }

    // [I@c818063, [I@c818063, [I@c818063, [I@c818063, [I@c818063
    static void badFor4() {
        int[] ints = {1, 2, 3, 4, 5};
        for (int anInt : ints) {
            System.out.println(ints);
        }
    }

    // doesn't compile
    static void badFor5() {
//        int[] ints = {1, 2, 3, 4, 5};
//        for (int int : ints) {
//            System.out.println(ints);
//        }
    }

    static void badFor6() {
        int[] ints = {1, 2, 3, 4, 5};
        Arrays.stream(ints).forEach(value -> System.out.println(value));
    }



}