疯狂javaj讲义第八章练习

第一题:

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Homework1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashSet arrSet=new HashSet();
        Scanner s=new Scanner(System.in);
        //while(s.hasNext())
        for(int i=0;i<5;i++)
        {
            arrSet.add(s.next());
        }
        System.out.println(arrSet);

    }

}

第二题:


import java.awt.List;
import java.util.ArrayList;

public class Homework2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] books={"111","222","333","444","555"
                ,"666","888","999","101010"};
        ArrayList bookList=new ArrayList();
        for(int i=0;i<books.length;i++)
        {
            bookList.add(books[i]);
        }
        //获取索引为5处的元素
        System.out.println(bookList.get(5));
        //获取其中某两个元素的索引
        System.out.println(bookList.indexOf("444"));
        System.out.println(bookList.indexOf("101010"));
        //删除索引为3的元素
        System.out.println(bookList.remove(3));
        System.out.println(bookList);

    }

}

第三题


import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;

public class Homework3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] str={"a","b","a","b","c","a","b","c","b"};
        HashMap hm=new HashMap();
        for(int i=0;i<str.length;i++)
        {
            hm.put(str[i],0);
            
        }
        System.out.print(hm);
        int temp1=0;
        for(int i=0;i<str.length;i++)
        {
            if(str[i]=="a")
            {
                temp1++;
                hm.put("a", temp1);
            }
        }
        System.out.print(hm);
        int temp2=0;
        for(int i=0;i<str.length;i++)
        {
            if(str[i]=="b")
            {
                temp2++;
                hm.put("b", temp2);
            }
        }
        System.out.print(hm);
        int temp3=0;
        for(int i=0;i<str.length;i++)
        {
            if(str[i]=="c")
            {
                temp3++;
                hm.put("c", temp3);
            }
        }
        System.out.print(hm);

    }

}

推荐阅读更多精彩内容