java Collections.copy的使用

拷贝一个ArrayList对象到另一个ArrayList对象中,正好发现Collections有一个copy方法。可是不好用啊总是报错。查看api才知道,它的capacity(容纳能力大小)可以指定(最好指定)。而初始化时size的大小永远默认为0,只有在进行add和remove等相关操作 时,size的大小才变化。然而进行copy()时候,首先做的是将desc的size和src的size大小进行比较,只有当desc的 size 大于或者等于src的size时才进行拷贝,否则抛出IndexOutOfBoundsException异常; 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class test1 {

 /**
  * Collections.copy()使用的小技巧

  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  List<String> src = new ArrayList<String>();
  src.add("111");
  src.add("222");
  src.add("333");
  src.add("444");
  List<String> dic = new ArrayList<String>(Arrays.asList(new String[src
    .size()]));
  Collections.copy(dic, src);
  for (Object s : dic) {
   System.out.println(s);
  }
 }

}

第二种办法: 
dest1  =   new  ArrayList(); 
Collections.addAll(dest1,  new  Object[src1.size()]); 
Collections.copy(dest1, src1); 

public static void main(String[] args) {
  // TODO Auto-generated method stub
  List<String> src = new ArrayList<String>();
  src.add("111");
  src.add("222");
  src.add("333");
  src.add("444");
  List<String> dest1  =   new  ArrayList<String>();
  Collections.addAll(dest1,  new  String[src.size()]);
  Collections.copy(dest1, src);
  for (Object s : dest1) {
   System.out.println(s);
  }


  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Android development is hot, and many programmers are interested in joining the fun. However, because this technology is based on Java, you should first obtain a solid grasp of the Java language and its foundational APIs to improve your chances of succeeding as an Android app developer. After all, you will be busy learning the architecture of an Android app, the various Android-specific APIs, and Android-specific tools. If you do not already know Java fundamentals, you will probably end up with a massive headache from also having to quickly cram those fundamentals into your knowledge base. Learn Java for Android Developmentteaches programmers of any skill level the essential Java language and foundational Java API skills that must be learned to improve the programmer''s chances of succeeding as an Android app developer. Each of the book''s 10 chapters provides an exercise section that gives you the opportunity to reinforce your understanding of the chapter''s material. Answers to the book''s more than 300 exercises are provided in an appendix. Once you complete this book, you will be ready to dive into Android, and you can start that journey by obtaining a copy ofBeginning Android 2. Additionally, author Jeff Friesen will provide supplementary material (such as 6 more chapters) on his javajeff.mb.ca website, available over the next few months following this book''s release. What you''ll learn The Java language: This book provides complete coverage of nearly every pre-Java version 7 language feature (native methods are briefly mentioned but not formally covered). Starting with those features related to classes and objects, you progress to object-oriented features related to inheritance, polymorphism, and interfaces. You then explore the advanced language features for nested types, packages, static imports, exceptions, assertions, annotations, generics, and enums. Continuing, you investigate strictfp, class literals, synchronized, volatile, the enhanced for loop statement, autoboxing/unboxing, and transient fields. The book also briefly presents most (if not all) of Java version 7''s language features, although not much is said about closures or modules (which were not finalized at the time of writing). Java APIs: In addition to Object and APIs related to exceptions, you explore Math, StrictMath, BigDecimal, BigInteger, Package, Boolean, Character, Byte, Short, Integer, Long, Float, Double, Number, the References API, the Reflection API, String, StringBuffer, System, the Threading API, the collections framework, the concurrency utilities, the internationalization APIs, the Preferences API, Random, the Regular Expressions API, File, RandomAccessFile, stream classes, and writer/reader classes. You will also get a tiny taste of Swing in the context of internationalization. Tools: You will learn how to use the JDK''s javac (compiler), java (application launcher), javadoc (Java documentation generator), and jar (Java archive creator, updater, and extractor) tools. You will also receive an introduction to the NetBeans and Eclipse integrated development environments. Although you can develop Android apps without NetBeans or Eclipse, working with these IDEs is much more pleasant. Who this book is for This book is for any programmer (including existing Java programmers and Objective-C (iPhone/iPad) programmers) of any skill level who needs to obtain a solid understanding of the Java language and foundational Java APIs before jumping into Android app development. Table of Contents Getting Started with Java Learning Language Fundamentals Learning Object-Oriented Language Features Mastering Advanced Language Features Part 1 Mastering Advanced Language Features Part 2 Exploring the Basic APIs Part 1 Exploring the Basic APIs Part 2 Discovering the Collections Framework Discovering Additional Utility APIs Performing I/O Solutions to Exercises

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值