瘋狂javaj講義第八章練習(xí)

第一題:

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));
        //獲取其中某兩個(gè)元素的索引
        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);

    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容