`

11、Holding--List

UP 
阅读更多
public class ListFeatures {
  public static void main(String[] args) {
	Random rand = new Random(47);
    List<Pet> pets = Pets.arrayList(7);
    print("1: " + pets);// 1: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug]
    Hamster h = new Hamster();
    pets.add(h); // Automatically resizes
    print("2: " + pets); //2: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Hamster]
    /*
     * contains() indexOf() remove()这些方法都要用到equals方法
     */
    print("3: " + pets.contains(h));//	      3: true
    pets.remove(h); // Remove by object 将对象的引用传递给remove方法
    Pet p = pets.get(2);
    print("4: " +  p + " " + pets.indexOf(p));// 4: Cymric 2 ##indexOf发现对象在list中的索引位置
    Pet cymric = new Cymric();
    print("5: " + pets.indexOf(cymric));//	      5: -1
    print("6: " + pets.remove(cymric));//	      6: false
    // Must be the exact object:
    print("7: " + pets.remove(p));//	      7: true
    print("8: " + pets);//8: [Rat, Manx, Mutt, Pug, Cymric, Pug]
    pets.add(3, new Mouse()); // Insert at an index
    print("9: " + pets);//	      9: [Rat, Manx, Mutt, Mouse, Pug, Cymric, Pug]
    
    /*
     * subList(int,int)方法从较大的List中很容易的创建出一个片段  产生的列表幕后是原始的列表
     * 索引对subList方法返回的列表的修改都会反映到初始列表中。
     * containsAll方法对参数的集合中元素的顺序并不关心 ,因为它是对作为参数的集合进行遍历 查看是否存在各个元素
     */
    List<Pet> sub = pets.subList(1, 4);
    print("subList: " + sub);//	      subList: [Manx, Mutt, Mouse]
    print("10: " + pets.containsAll(sub));//	      10: true
    Collections.sort(sub); // In-place sort
    print("sorted subList: " + sub);//sorted subList: [Manx, Mouse, Mutt]
    // Order is not important in containsAll():
    print("11: " + pets.containsAll(sub));//	  11: true
    Collections.shuffle(sub, rand); // Mix it up
    print("shuffled subList: " + sub);//	  shuffled subList: [Mouse, Manx, Mutt]
    
    print("12: " + pets.containsAll(sub));//	  12: true
    
    List<Pet> copy = new ArrayList<Pet>(pets);
    sub = Arrays.asList(pets.get(1), pets.get(4));
    print("sub: " + sub);//	  sub: [Mouse, Pug]
    
    copy.retainAll(sub);//依赖于equals()方法 返回的值是(copy是否是sub的超集)既是否从copy中移除了在sub中没有的数据
    print("13: " + copy);//	  13: [Mouse, Pug]
    copy = new ArrayList<Pet>(pets); // Get a fresh copy
    copy.remove(2); // Remove by index
    print("14: " + copy);//	  14: [Rat, Mouse, Mutt, Pug, Cymric, Pug]
    copy.removeAll(sub); // copy中移除参数中包括的所有的元素
    print("15: " + copy);//	  15: [Rat, Mutt, Cymric, Pug]
    copy.set(1, new Mouse()); // Replace an element
    print("16: " + copy);//	  16: [Rat, Mouse, Cymric, Pug]
    copy.addAll(2, sub); // 在指定的索引处插入参数中的所有
    print("17: " + copy);//	  17: [Rat, Mouse, Mouse, Pug, Cymric, Pug]
    print("18: " + pets.isEmpty());//	  18: false
    pets.clear(); // Remove all elements
    print("19: " + pets);//	  19: []
    print("20: " + pets.isEmpty());//	  20: true
    pets.addAll(Pets.arrayList(4));
    print("21: " + pets);//	  21: [Manx, Cymric, Rat, EgyptianMau]
    Object[] o = pets.toArray();//创建合适大小的数组
    print("22: " + o[3]);//	  22: EgyptianMau
    Pet[] pa = pets.toArray(new Pet[0]);//目标类型的数据
    print("23: " + pa[3].id());//	  23: 14  
  } 
} 

 

分享到:
评论

相关推荐

    Lerner -- Python Workout. 50 Essential Exercises -- 2020.pdf

    11. **Alphabetizing Names** - **Objective:** Sort a list of names alphabetically. - **Key Concepts:** - Sorting lists using the `sort()` method or `sorted()` function. - Custom sorting based on ...

    libusb-1.0.9

    //holding number of devices in list r = libusb_init(&ctx;); //initialize a library session if(r ) { cout; //there was an error return 1; } libusb_set_debug(ctx, 3); //set verbosity level to 3, ...

    Reflector反编译器

    Assembly Cache: When resolving an assembly reference, Reflector will first search the local path next to the assembly holding the reference and then falls back to the cache directories defined in the ...

    反编译工具

    Assembly Cache: When resolving an assembly reference, Reflector will first search the local path next to the assembly holding the reference and then falls back to the cache directories defined in the ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    (FActionIndex could appear beyond the list boundaries). Thanks to Giedrius Matonis. - ADD: Added the method TCustomProp.Assign - a source flex-property is written to stream, which is then read by...

    Introdution to R-Notes

    Ruowen WangIntroduction to R2022-12-1112 / 148Introduction to ListsLists are the most flexible data structure in R, capable of holding objects of different types. They are essentially ordered ...

    IS-IS三步曲之3 配置篇

    使用`access-list`配合`distribute-list`命令进行路由过滤。 ##### 实验七:在NBMA上配置IS-IS - **目标**: 了解如何在非广播多路访问(NBMA)网络上配置IS-IS。 - **步骤**: 1. 配置IS-IS的基本参数。 2. 使用`...

    NGUI Next-Gen UI 3.0.7 f1.unitypackage

    - NEW: Improved the Text List's functionality, adding support for touch interaction and having a scroll bar. - NEW: Recreated the Chat Window example -- it now features a resizable chat window. - NEW:...

    layouts-event-list-widget:使用“事件列表”小组件,您可以使用Pavilion的“自定义布局”插件在边栏中显示即将发生的事件

    :people_holding_hands: 切换被邀请者选择是否或如何在布局中显示被邀请者。 您可以让被邀请者仅在悬停状态下显示(默认操作),使其始终显示或完全禁用它们。 以下是每种类型在默认主题上的外观的屏幕截图。 残障...

    Android代码-talon-for-twitter-android

    I am lucky enough to say that, as a paid app, Talon has perched itself at the top of the top paid social apps list since it's release in 2014. It has amassed 100,000 downloads while holding a 4.6 star...

    ak2新版内核AKAIO1.5

    NOTE: DMA-Save mode is now default so DMA mode (red loading text) is now enabled by holding X during loading. AK-AIO 1.4 + Settings window uses tabs now, merging Settings, Advanced Settings, and ...

    :two_men_holding_hands: :two_women_holding_hands: 通过使用 HoC 而不是组件来进行 A/B 测试的糟糕侵入方式

    用法 安装 $ npm install --save rn-ab-hoc Component /* list.js */ import abConnect from 'rn-ab-hoc';从 './flatList.js' 导入 FlatList;导入 ListView rom './listView.js';导入 OtherList rom './otherList....

    NET Reflector 5.0 Release 及19款插件集合

    Assembly Cache: When resolving an assembly reference, Reflector will first search the local path next to the assembly holding the reference and then falls back to the cache directories defined in the ...

    ora分析脚本

    Usage: ora [-u user] [-i instance#] &lt;command&gt; [] General -u user/pass use USER/PASS to log in -i instance# append # to ORACLE_SID -sid &lt;sid&gt; set ORACLE_SID to sid -top # limit some large queries ...

    reflector for net C# 反编译利器

    Assembly Cache:When resolving an assembly reference, Reflector will first search the local path next to the assembly holding the reference and then falls back to the cache directories defined in the ...

    反编译软件

    &lt;br&gt;Assembly Cache: When resolving an assembly reference, Reflector will first search the local path next to the assembly holding the reference and then falls back to the cache directories ...

    Manning-AspectJInAction

    my to-do list, and I started looking at it again when it was in version 0.8. By then, AspectJ had evolved into a much more powerful language. I started using AspectJ and found that the more I used it,...

    OutlookAttachView v2.73

    sorting, by holding down the shift key while clicking the column header. Be aware that you only have to hold down the shift key when clicking the second/third/fourth column. To sort the first ...

    Tinning the Tabs-crx插件

    A temporary holding and management for Google Chrome tabs. The totally new designed user interface enhance the interaction and practicability of this extension. Moreover, It can remember the last ...

    微软内部资料-SQL性能优化5

     Chapter 11: “Batches, Stored Procedures and Functions”, Inside SQL Server 2000 by Kalen Delaney Finding Rows without Indexes With No Indexes, A Table Must Be Scanned SQL Server keeps track ...

Global site tag (gtag.js) - Google Analytics