POLAYOR

学校Java Week5

Week5

W5L1

Review

  • Control Flow
  • Condition Loops
  • counter Loops

For loops

Does the exact same thing with less code

for (int i = 0; i < 10; i++)
// initial value; condition:continue to loop; change

Nested For loops

for () {
    for(){
        ......
    }
}

//print 1-100
public class Forloops_1to100 {
    public static void main(String[] args) {
        for (int i = 1; i<11; i++) {
            for (int j = 1; j<11; j++) {
                System.out.print(i*j+"\t");
            }
            System.out.println();
        }
    }
}

Arrays

After created, the size can not be changed.

An array of primitives

int [] myArray = new int[5];
int [] myArray; // Declare
myArray = new int[5]; // Get a new array object with memory for 5 ints. Assign to myArray

Array Types

String[] myArray = {"a", "b"}
boolean[] myArray = {true, false}

String[] s = new String[10]; // default values: null
boolean[] b = new boolean[4]; // default values: false
int[] i = new int[10]; // default values: 0

W5L2 ex1

public class W5L2_ex1 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6};
        reverseInts(nums);
        printInts(nums);
    }

    public static void reverseInts(int[] nums) {
        int t = 0;
        int l = nums.length;
        for (int i=0; i<l/2; i++) {
            t = nums[i];
            nums[i] = nums[l-i-1];
            nums[l-i-1] = t;
        }
    }

    public static void printInts(int[] nums) {
        System.out.print("[");

        int l = nums.length;
        for (int i=0; i<l; i++) {
            if (i==0) {
                System.out.print(nums[i]);
            } else {
                System.out.print(", " + nums[i]);
            }
        }

        System.out.print("]");
    }
}

W5L2 ex2

public class W5L2_ex2 {
    public static void main(String[] args) {
        int [] nums = {1, 2, 3};
        nums = copyEven(nums);
        printInts(nums);
    }

    public static int[] copyEven(int[] nums) {
        int l = nums.length;
        int[] even = new int[(l+1)/2];
        int c = 0;

        for (int i=0; i<l; i++) {
            if (i%2 == 0) {
                even[c] = nums[i];
                c++;
            }
        }

        return even;
    }

    public static void printInts(int[] nums) {
        System.out.print("[");

        int l = nums.length;
        for (int i=0; i<l; i++) {
            if (i==0) {
                System.out.print(nums[i]);
            } else {
                System.out.print(", " + nums[i]);
            }
        }

        System.out.print("]");
    }
}

W5L2 ex3

public class W5L2_ex3 {
    // 两种方法均能解决
    public static void main(String[] args) {
        diamond(9);
        diamondArray(9);
    }

    public static void diamond(int n) {
        for (int i=1; i<n; i += 2) {
            int j = (n-i)/2;
            int t = j;
            int k = i;

            while (j>0) {
                if (j == (n-i)/2) {
                    System.out.print(".");
                } else {
                    System.out.print(" .");
                }
                j--;
            }

            while (k>0) {
                System.out.print(" *");
                k--;
            }

            while (t>0) {
                System.out.print(" .");
                t--;
            }

            System.out.println();
        }

        for (int i=n; i>=1; i -= 2) {
            int j = (n-i)/2;
            int t = j;
            int k = i;

            while (j>0) {
                if (j == (n-i)/2) {
                    System.out.print(".");
                } else {
                    System.out.print(" .");
                }
                j--;
            }

            while (k>0) {
                if (k == n) {
                    System.out.print("*");
                    k--;
                    while (k>0) {
                        System.out.print(" *");
                        k--;
                    }
                    break;
                }

                System.out.print(" *");
                k--;
            }

            while (t>0) {
                System.out.print(" .");
                t--;
            }

            System.out.println();
        }
    }

    public static void diamondArray(int n) {
        int[] indexArray = new int[n];
        for (int i=0; i<n; i+=2) {
            int t = i;
            int first = (n+2-i)/2 - 1;
            for (int c=0; c<=i; c++) {
                indexArray[first+c] = 1;
            }

            for (int j=0; j<n; j++) {
                if (indexArray[j] == 0) {
                    if (j==0) {
                        System.out.print(".");
                    } else {
                        System.out.print(" .");
                    }

                } else {
                    if (t==n-1) {
                        System.out.print("*");

                        while (t>0) {
                            System.out.print(" *");
                            t--;
                        }

                        break;
                    }
                    System.out.print(" *");
                }
            }

            System.out.println();
        }

        for (int i=n-2; i>0; i-=2) {
            int left_last = (n+2-i)/2 - 2;
            int right_first = (n+2+i)/2 - 1;

            for (int c=0; c<=left_last; c++) {
                indexArray[c] = 0;
            }

            for (int c=right_first; c<=n-1; c++) {
                indexArray[c] = 0;
            }

            for (int j=0; j<n; j++) {
                if (indexArray[j] == 0) {
                    if (j==0) {
                        System.out.print(".");
                    } else {
                        System.out.print(" .");
                    }

                } else {
                    System.out.print(" *");
                }
            }

            System.out.println();
        }
    }
}

W5L2 ex4

public class W5L2_ex4 {
    public static void main(String[] args) {
        printInts(arithSeries(4));
    }

    public static int[] arithSeries(int n) {
        int[] myArray = new int[n*(n + 1)/2];
        int index = 0;

        for (int i=1; i<=n; i++) {
            for (int j=1; j<=i; j++) {
                myArray[index] = j;
                index++;
            }
        }

        return myArray;
    }

    public static void printInts(int[] nums) {
        System.out.print("[");

        int l = nums.length;
        for (int i=0; i<l; i++) {
            if (i==0) {
                System.out.print(nums[i]);
            } else {
                System.out.print(", " + nums[i]);
            }
        }

        System.out.print("]");
    }
}

W5 CW1 5.1

public class W5_CW1_51 {
    public static void main(String[] args) {
        bandMatrix(8,0);
        bandMatrix(8,1);
        bandMatrix(8,2);
        bandMatrix(8,3);
        bandMatrix(8,4);
        bandMatrix(8,5);
        bandMatrix(8,6);
        bandMatrix(8,7);
    }

    public static void bandMatrix(int n, int width) {
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                if (j == 0) {
                    int ij = i+j;
                    if (Math.abs(ij-2*i) <= width) {
                        System.out.print("*");
                    } else {
                        System.out.print(0);
                    }
                } else {
                    int ij = i+j;
                    if (Math.abs(ij-2*i) <= width) {
                        System.out.print("  *");
                    } else {
                        System.out.print("  " + 0);
                    }
                }
            }

            System.out.println();
        }
    }
}

W5 CW1 5.2

public class w5_cw1_52 {
    public static void main(String[] args) {
        int[] nums1 = {1, 2, 3, 4};
        printInts(evenOddSwap(nums1));
        int[] nums2 = {100, 25};
        printInts(evenOddSwap(nums2));
        int[] nums3 = {};
        printInts(evenOddSwap(nums3));
        int[] nums4 = {11, 55, 100, 200, 300, 7};
        printInts(evenOddSwap(nums4));
    }

    public static int[] evenOddSwap(int[] nums) {
        int l = nums.length;
        boolean[] flag = new boolean[l];
        int t = 0;
        for (int i=0; i<l; i++) {
            if (flag[i]) {
                continue;
            } else {
                flag[i] = true;
            }

            if (nums[i]%2 == 0) {
                for (int j=i; j<l; j++) {
                    if (nums[j]%2 == 1) {
                        t = nums[j];
                        nums[j] = nums[i];
                        nums[i] = t;
                        flag[j] = true;
                        break;
                    }
                }
            } else {
                for (int j=i; j<l; j++) {
                    if (nums[j]%2 == 0) {
                        t = nums[j];
                        nums[j] = nums[i];
                        nums[i] = t;
                        flag[j] = true;
                        break;
                    }
                }
            }
        }

        return nums;
    }

    public static void printInts(int[] nums) {
        System.out.print("[");

        int l = nums.length;
        for (int i=0; i<l; i++) {
            if (i==0) {
                System.out.print(nums[i]);
            } else {
                System.out.print(", " + nums[i]);
            }
        }

        System.out.print("]");
    }
}

posted on 2022-10-11 21:43  POLAYOR  阅读(48)  评论(0编辑  收藏  举报

导航