日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ArrayList刷题总结

發(fā)布時間:2024/8/26 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ArrayList刷题总结 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • list中添加,獲取,刪除元素;
  • list中是否包含某個元素;
  • list中根據索引將元素數值改變(替換);
  • list中查看(判斷)元素的索引;
  • 根據元素索引位置進行的判斷;
  • 利用list中索引位置重新生成一個新的list(截取集合);
  • 對比兩個list中的所有元素;
  • 判斷l(xiāng)ist是否為空;
  • 返回Iterator集合對象;
  • 將集合轉換為字符串;
  • 將集合轉換為數組;
  • 集合類型轉換;
  • 轉載自http://hovertree.com/h/bjaf/pk4rwg78.htm

    1.list中添加,獲取,刪除元素;

    添加方法是:.add(e);  

    獲取方法是:.get(index);  

    刪除方法是:

    .remove(index);按照索引刪除;

    .remove(Object o); 按照元素內容刪除;

    List<String> person=new ArrayList<>();person.add("jackie"); //索引為0 //.add(e)person.add("peter"); //索引為1person.add("annie"); //索引為2person.add("martin"); //索引為3person.add("marry"); //索引為4person.remove(3); //.remove(index)person.remove("marry"); //.remove(Object o)String per="";per=person.get(1);System.out.println(per); .get(index)for (int i = 0; i < person.size(); i++) {System.out.println(person.get(i)); //.get(index)}

    ?

    2.list中是否包含某個元素;

      方法:.contains(Object o); 返回true或者false

    List<String> fruits=new ArrayList<>();fruits.add("蘋果");fruits.add("香蕉");fruits.add("桃子");//for循環(huán)遍歷listfor (int i = 0; i < fruits.size(); i++) {System.out.println(fruits.get(i));}String appleString="蘋果";//true or falseSystem.out.println("fruits中是否包含蘋果:"+fruits.contains(appleString));if (fruits.contains(appleString)) {System.out.println("我喜歡吃蘋果");}else {System.out.println("我不開心");}

    ?

    3.list中根據索引將元素數值改變(替換);

      注意 .set(index, element); 和 .add(index, element); 的不同;

    String a="白龍馬", b="沙和尚", c="八戒", d="唐僧", e="悟空";List<String> people=new ArrayList<>();people.add(a);people.add(b);people.add(c);people.set(0, d); //.set(index, element); //將d唐僧放到list中索引為0的位置,替換a白龍馬people.add(1, e); //.add(index, element); //將e悟空放到list中索引為1的位置,原來位置的b沙和尚后移一位//增強for循環(huán)遍歷listfor(String str:people){System.out.println(str);}

    ?

    4.list中查看(判斷)元素的索引;  

      注意:.indexOf(); 和 ?lastIndexOf()的不同;

    List<String> names=new ArrayList<>();names.add("劉備"); //索引為0names.add("關羽"); //索引為1names.add("張飛"); //索引為2names.add("劉備"); //索引為3names.add("張飛"); //索引為4System.out.println(names.indexOf("劉備"));System.out.println(names.lastIndexOf("劉備"));System.out.println(names.indexOf("張飛"));System.out.println(names.lastIndexOf("張飛"));

    ?

    5.根據元素索引位置進行的判斷;

    if (names.indexOf("劉備")==0) {System.out.println("劉備在這里"); }else if (names.lastIndexOf("劉備")==3) {System.out.println("劉備在那里"); }else {System.out.println("劉備到底在哪里?"); }

    ?

    6.利用list中索引位置重新生成一個新的list(截取集合);

      方法: .subList(fromIndex, toIndex);  .size() ; 該方法得到list中的元素數的和

    List<String> phone=new ArrayList<>();phone.add("三星"); //索引為0phone.add("蘋果"); //索引為1phone.add("錘子"); //索引為2phone.add("華為"); //索引為3phone.add("小米"); //索引為4//原list進行遍歷for(String pho:phone){System.out.println(pho);}//生成新listphone=phone.subList(1, 4); //.subList(fromIndex, toIndex) //利用索引1-4的對象重新生成一個list,但是不包含索引為4的元素,4-1=3for (int i = 0; i < phone.size(); i++) { // phone.size() 該方法得到list中的元素數的和 System.out.println("新的list包含的元素是"+phone.get(i));}

    ?

    7.對比兩個list中的所有元素;

      //兩個相等對象的equals方法一定為true, 但兩個hashcode相等的對象不一定是相等的對象

    //1.
    if (person.equals(fruits)) {System.out.println("兩個list中的所有元素相同"); }else {System.out.println("兩個list中的所有元素不一樣"); } //2. if (person.hashCode()==fruits.hashCode()) {System.out.println("我們相同"); }else {System.out.println("我們不一樣"); }

    ?

    8.判斷l(xiāng)ist是否為空;

      //空則返回true,非空則返回false

    if (person.isEmpty()) {System.out.println("空的"); }else {System.out.println("不是空的"); }

    ?

    9.返回Iterator集合對象;

    System.out.println("返回Iterator集合對象:"+person.iterator());

    ?

    1+0.將集合轉換為字符串;

    String liString=""; liString=person.toString(); System.out.println("將集合轉換為字符串:"+liString);

    ?

    11.將集合轉換為數組;

    System.out.println("將集合轉換為數組:"+person.toArray());

    ?

    12.集合類型轉換;

    //1.默認類型 List<Object> listsStrings=new ArrayList<>();for (int i = 0; i < person.size(); i++) {listsStrings.add(person.get(i)); } //2.指定類型 List<StringBuffer> lst=new ArrayList<>();for(String string:person){lst.add(StringBuffer(string)); }

    ?

    附完整代碼:

    package MyTest01;import java.util.ArrayList; import java.util.List;public class ListTest01 {public static void main(String[] args) {//list中添加,獲取,刪除元素List<String> person=new ArrayList<>();person.add("jackie"); //索引為0 //.add(e)person.add("peter"); //索引為1person.add("annie"); //索引為2person.add("martin"); //索引為3person.add("marry"); //索引為4person.remove(3); //.remove(index)person.remove("marry"); //.remove(Object o)String per="";per=person.get(1);System.out.println(per); .get(index)for (int i = 0; i < person.size(); i++) {System.out.println(person.get(i)); //.get(index)}//list總是否包含某個元素List<String> fruits=new ArrayList<>();fruits.add("蘋果");fruits.add("香蕉");fruits.add("桃子");//for循環(huán)遍歷listfor (int i = 0; i < fruits.size(); i++) {System.out.println(fruits.get(i));}String appleString="蘋果";//true or falseSystem.out.println("fruits中是否包含蘋果:"+fruits.contains(appleString));if (fruits.contains(appleString)) {System.out.println("我喜歡吃蘋果");}else {System.out.println("我不開心");}//list中根據索引將元素數值改變(替換)String a="白龍馬", b="沙和尚", c="八戒", d="唐僧", e="悟空";List<String> people=new ArrayList<>();people.add(a);people.add(b);people.add(c);people.set(0, d); //.set(index, element) //將d唐僧放到list中索引為0的位置,替換a白龍馬people.add(1, e); //.add(index, element); //將e悟空放到list中索引為1的位置,原來位置的b沙和尚后移一位//增強for循環(huán)遍歷listfor(String str:people){System.out.println(str);}//list中查看(判斷)元素的索引List<String> names=new ArrayList<>();names.add("劉備"); //索引為0names.add("關羽"); //索引為1names.add("張飛"); //索引為2names.add("劉備"); //索引為3names.add("張飛"); //索引為4System.out.println(names.indexOf("劉備"));System.out.println(names.lastIndexOf("劉備"));System.out.println(names.indexOf("張飛"));System.out.println(names.lastIndexOf("張飛"));//根據元素索引位置進行的判斷if (names.indexOf("劉備")==0) {System.out.println("劉備在這里");}else if (names.lastIndexOf("劉備")==3) {System.out.println("劉備在那里");}else {System.out.println("劉備到底在哪里?");}//利用list中索引位置重新生成一個新的list(截取集合)List<String> phone=new ArrayList<>();phone.add("三星"); //索引為0phone.add("蘋果"); //索引為1phone.add("錘子"); //索引為2phone.add("華為"); //索引為3phone.add("小米"); //索引為4//原list進行遍歷for(String pho:phone){System.out.println(pho);}//生成新listphone=phone.subList(1, 4); //.subList(fromIndex, toIndex) //利用索引1-4的對象重新生成一個list,但是不包含索引為4的元素,4-1=3for (int i = 0; i < phone.size(); i++) { // phone.size() 該方法得到list中的元素數的和 System.out.println("新的list包含的元素是"+phone.get(i));}//對比兩個list中的所有元素//兩個相等對象的equals方法一定為true, 但兩個hashcode相等的對象不一定是相等的對象if (person.equals(fruits)) {System.out.println("兩個list中的所有元素相同");}else {System.out.println("兩個list中的所有元素不一樣");}if (person.hashCode()==fruits.hashCode()) {System.out.println("我們相同");}else {System.out.println("我們不一樣");}//判斷l(xiāng)ist是否為空//空則返回true,非空則返回falseif (person.isEmpty()) {System.out.println("空的");}else {System.out.println("不是空的");}//返回Iterator集合對象System.out.println("返回Iterator集合對象:"+person.iterator());//將集合轉換為字符串String liString="";liString=person.toString();System.out.println("將集合轉換為字符串:"+liString);//將集合轉換為數組,默認類型System.out.println("將集合轉換為數組:"+person.toArray());將集合轉換為指定類型(友好的處理)//1.默認類型List<Object> listsStrings=new ArrayList<>();for (int i = 0; i < person.size(); i++) {listsStrings.add(person.get(i));}//2.指定類型List<StringBuffer> lst=new ArrayList<>();for(String string:person){lst.add(StringBuffer(string));}}private static StringBuffer StringBuffer(String string) {return null;}}

    ?

    轉載于:https://www.cnblogs.com/SkyeAngel/p/9308715.html

    與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的ArrayList刷题总结的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。