《Java語言程序設計與數據結構》編程練習答案(第十九章)(一)

《Java語言程序設計與數據結構》編程練習答案(第十九章)(一)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

19.1

public class book {


    public static void main(String[] args) {
        GenericStack<Integer> testStack = new GenericStack<Integer>();
        for (int i=0;i<10;i++){
            testStack.push(i);
        }
        for (int i=0;i<10;i++){
            System.out.println(testStack.pop());
        }
    }
}

class GenericStack<T>{
    private T[] list = (T[])new Object[4];
    private int capacity = 4;
    private int size = 0;

    public int getSize(){
        return this.size;
    }

    public T peek(){
        return list[size-1];
    }

    public void push(T obj){
        if(size == capacity){
            //expand
            capacity *= 2;
            T[] newList = (T[])new Object[capacity];
            System.arraycopy(list, 0, newList, 0, list.length);
            list = newList;
        }
        list[size] = obj;
        size++;
    }

    public T pop(){
        T ret = list[size-1];
        size--;
        return ret;
    }

    public boolean isEmpty(){
        return size == 0;
    }

}

19.2

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        GenericStack<String> testStack = new GenericStack<String>();
        System.out.println("Please enter 5 strings:");
        for(int i=0;i<5;i++){
            String tmp = input.nextLine();
            testStack.push(tmp);
        }
        System.out.println("Throwing out:");
        for(int i=0;i<5;i++){
            System.out.println(testStack.pop());
        }
    }
}

class GenericStack<T> extends ArrayList<T>{

    public T peek(){
        return this.get(this.size()-1);
    }

    public void push(T obj){
        this.add(obj);
    }

    public T pop(){
        T ret = this.get(this.size()-1);
        this.remove(this.size()-1);
        return ret;
    }
}

19.3

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        ArrayList<Integer> test = new ArrayList<>();
        test.add(114);
        test.add(514);
        test.add(19);
        test.add(19);
        test.add(810);
        ArrayList<Integer> result = removeDuplicates(test);
        for(int i=0;i<result.size();i++){
            System.out.println(result.get(i));
        }
    }

    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list){
        return new ArrayList<T>(new HashSet<T>(list));
    }
}

19.4

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        Integer[] test = new Integer[10];
        for(int i=0;i<10;i++){
            test[i] = i;
        }
        System.out.println(linearSearch(test, 5));

    }

    public static <T extends Comparable<T>> int linearSearch(T[] list, T key){
        int ret = -1;
        for(int i=0;i<list.length;i++){
            if(key.compareTo(list[i]) == 0){
                ret = i;
                break;
            }
        }
        return ret;
    }
}

19.5

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        Integer[] test = new Integer[10];
        for(int i=0;i<10;i++){
            test[i] = i;
        }
        System.out.println(max(test));

    }

    public static <T extends Comparable<T>> T max(T[] list){
        T ret = list[0];
        for(int i=0;i<list.length;i++){
            if(ret.compareTo(list[i]) < 0){
                ret = list[i];
            }
        }
        return ret;
    }
}

19.6

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        Integer[][] test = {{1,1,4,5,1,4},{1,9,1,9,8,1}};
        System.out.println(max(test));
    }

    public static <T extends Comparable<T>> T max(T[][] list){
        T ret = list[0][0];
        for (T[] ts : list) {
            for (int j = 0; j < list[0].length; j++) {
                if (ret.compareTo(ts[j]) < 0) {
                    ret = ts[j];
                }
            }
        }
        return ret;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章