Java语言程序设计-基础篇-第八版-第六章

时间:2021-01-02 11:45:48
 1 package chapter6一维数组;
 2 
 3 public class Arrays类 {
 4     public static void main(String[] args){
 5         
 6         double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5};
 7         java.util.Arrays.sort(numbers);
 8         
 9         int[] list = {2, 4, 7, 10};
10         System.out.println(java.util.Arrays.binarySearch(list, 4));
11         
12         char[] chars = {'a', 'A', '4', 'F', 'D', 'p'};
13         java.util.Arrays.sort(chars, 1, 3);
14         
15         int[] list1 = {2, 4, 7, 10};
16         int[] list2 = {2, 4, 7, 10};
17         int[] list3 = {4, 2, 7, 10};
18         System.out.println(java.util.Arrays.equals(list1, list2));
19         System.out.println(java.util.Arrays.equals(list2, list3));
20     
21     }
22 
23 }
 1 package chapter6一维数组;
 2 
 3 public class BinarySearch {
 4         
 5     public static int binarySearch(int[] list, int key){
 6         int low = 0;
 7         int high = list.length - 1;
 8         
 9         while (high >= low) {
10             int mid = (low + high) / 2;
11             if (key < list[mid])
12                 high = mid - 1;
13             else if (key == list[mid])
14                 return mid;
15             else
16                 high = mid + 1;
17         }
18         
19         return -low - 1;
20     }
21     
22     public static void main(String[] args){
23         int[] list = {2, 4, 7, 10};
24         int j = binarySearch(list, 3);
25         System.out.println(j);
26     }
27 }
package chapter6一维数组;
import chapter5方法.RandomCharacter;
public class CountLettersInArray {
    
    public static void main(String[] args){
        char[] chars = createArray();
        
        System.out.println("The lowercase letters are: ");
        displayArray(chars);
        
        int[] counts = countLetters(chars);
        
        System.out.println();
        System.out.println("The occurrences of each letter are: ");
        displayCounts(counts);
    }
    
    public static char[] createArray(){
        char[] chars = new char[100];
        
        for (int i = 0; i < chars.length; i++)
            chars[i] = RandomCharacter.getRandomLowerCaseLetter();
        return chars;
    }
    
    public static void displayArray(char[] chars){
        for (int i = 0; i < chars.length; i++) {
            if ((i + 1) % 20 == 0)
                System.out.println(chars[i]);
            else
                System.out.print(chars[i] + " ");
        }
    }    
        
    public static int[] countLetters(char[] chars) {
        int[] counts = new int[26];
        
        for (int i = 0; i < chars.length; i++)
            counts[chars[i] - 'a']++;
        
        return counts;
    }    
    
    public static void displayCounts(int[] counts) {
        for (int i = 0; i < counts.length; i++) {
            if ((i + 1) % 10 == 0)
                System.out.println(counts[i] + " " + (char)(i + 'a'));
            else
                System.out.print(counts[i] + " " + (char)(i + 'a') + " ");
        }
    }
}
 1 package chapter6一维数组;
 2 
 3 public class DeckOfCards {
 4     public static void main(String[] args){
 5         int[] deck = new int[52];
 6         String[] suits = {"Spades", "Hearts", "Clubs", "Diamonds"};
 7         String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8",
 8                 "9", "10", "Jack", "Queen", "King"};
 9         
10         for (int i = 0; i < deck.length; i++)
11             deck[i] = i;
12         
13         for (int i = 0; i < deck.length; i++){
14             int index = (int)(Math.random() * deck.length);
15             int temp = deck[i];
16             deck[i] = deck[index];
17             deck[index] = temp;
18         }
19         
20         for (int i = 0; i < 4; i++){
21             String suit = suits[deck[i] / 13];
22             String rank = ranks[deck[i] % 13];
23             System.out.println("Card number " + deck[i] + ":" +
24               rank + " of " + suit);
25         }
26     }
27 }
 1 package chapter6一维数组;
 2 
 3 public class InsertionSort {
 4     public static void insertionSort(double[] list){
 5         for (int i = 1; i < list.length; i++){
 6             double currentElement = list[i];
 7             int k;
 8             for (k = i; k >= 0 && list[k] > currentElement; k--) {
 9                 list[k + 1] = list[k];
10             }
11             
12             list[k + 1] = currentElement;
13         }
14     }
15 }
 1 package chapter6一维数组;
 2 
 3 public class LinearSearch {
 4     public static int linearSearch(int[] list, int key){
 5         for (int i = 0; i < list.length; i++) {
 6             if (key == list[i])
 7                 return i;
 8         }
 9         
10         return -1;
11     }
12     
13     public static void main(String[] args){
14         int[] list = {2, 4, 7, 10};
15         int j = linearSearch(list, 9);
16         System.out.println(j);
17     }
18 
19 }
 1 package chapter6一维数组;
 2 
 3 public class LottoNumbers {
 4     public static void main(String[] args){
 5         java.util.Scanner input = new java.util.Scanner(System.in);
 6         boolean[] isCovered = new boolean[99];
 7         
 8         int number = input.nextInt();
 9         while (number != 0) {
10             isCovered[number - 1] = true;
11             number = input.nextInt();
12         }
13         
14         boolean allCovered = true;
15         for (int i = 0; i < 99; i++)
16             if (!isCovered[i]) {
17                 allCovered = false;
18                 break;
19             }
20         if (allCovered)
21             System.out.println("The tickets cover all numbers");
22         else
23             System.out.println("The tickets don't cover all numbers");
24     }
25 }
package chapter6一维数组;

public class SelectionSort {
    public static void selectSort(double[] list) {
        for (int i = 0; i < list.length - 1; i++) {
            double currentMin = list[i];
            int currentMinIndex = i;
            
            for (int j = i + 1; j < list.length; j++){
                if (currentMin > list[j]) {
                    currentMin = list[j];
                    currentMinIndex = j;
                }
            }
            
            
            list[currentMinIndex] = list[i];
            list[i] = currentMin;
        }
    }    
}
 1 package chapter6一维数组;
 2 
 3 public class TestPassArray {
 4     public static void main(String[] args){
 5         int[] a = {1, 2};
 6         
 7         System.out.println("Before invoking swap");
 8         System.out.println("array is {" + a[0] + ", " + a[1] + "}");
 9         swap(a[0], a[1]);
10         System.out.println("After invoking swap");
11         System.out.println("array is {" + a[0] + ", " + a[1] + "}");
12         
13         System.out.println("Before invoking swapFirstTwoInArray");
14         System.out.println("array is {" + a[0] + ", " + a[1] + "}");
15         swapFirstTwoInArray(a);
16         System.out.println("After invoking swapFirstTwoInArray");
17         System.out.println("array is {" + a[0] + ", " + a[1] + "}");
18     }
19     
20      public static void swap(int n1, int n2) {
21          int temp = n1;
22          n1 = n2;
23          n2 = temp;
24      }
25      
26      public static void swapFirstTwoInArray(int[] array) {
27          int temp = array[0];
28          array[0] = array[1];
29          array[1] = temp;
30      }
31 }
 1 package chapter6一维数组;
 2 
 3 public class VarArgsDemo {
 4    public static void main(String[] args){
 5        printMax(34, 3, 3, 2, 56.5);
 6        printMax(new double[]{ 1, 2, 3});
 7    }
 8    
 9    public static void printMax(double... numbers){
10        if (numbers.length == 0){
11            System.out.println("No argument passed");
12            return;
13        }
14        
15        double result = numbers[0];
16        
17        for (int i = 1; i < numbers.length; i++)
18         if (numbers[i] > result)
19             result = numbers[i];
20        
21        System.out.println("The max value is " + result);
22    }
23 }