|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
C#是不行的,比如说美国的航天飞船里就有java开发的程序以上是我的愚见,其实不管那种语言,你学好了,都能找到好的工作,
JAVA中的容器供应了十分完美的办法来保留对象,你可使用这些工具来办理年夜数据量的成绩。上面是笔者在开辟顶用到过的一些容器总结云云。
1ArrayList
利用ArrayList十分复杂:创立一个实例,用add()拔出对象,然后用get()会见这些对象,此时必要索引,就象数组一样,可是不必要方括号,ArrayList另有size()办法,从而能够晓得ArrayList的巨细,也能够制止由于越界而激发毛病。别的,ArrayList善于随机会见元素,可是在List的两头拔出和移处元素时较慢。上面是ArrayList的例子(来自thinkinginjava):
/**//*
*@(#)AppleAndOrangesWithGenerics.java 1.0May17,2008
*@author:Administrator
*Copyright2008CSSWEBMicrosystems,Inc.Allrightsreserved.
*CSSWEBROOMPROPRIETARY/CONFIDENTIAL.Useissubjecttolicenseterms.
*/
packageholding;
importjava.util.ArrayList;
publicclassAppleAndOrangesWithGenerics...{
publicstaticvoidmain(String[]args)...{
ArrayList<Apple>apples=newArrayList<Apple>();
for(inti=0;i<3;i++)...{
apples.add(newApple());
}
for(inti=0;i<apples.size();i++)
System.out.println(((Apple)apples.get(i)).id());
for(Applec:apples)
System.out.println(c.id()+"-");
}
}
/**//*
*@(#)ListFeatures.java 1.0May17,2008
*@author:Administrator
*Copyright2008CSSWEBMicrosystems,Inc.Allrightsreserved.
*CSSWEBROOMPROPRIETARY/CONFIDENTIAL.Useissubjecttolicenseterms.
*/
packageholding;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.List;
importjava.util.Random;
importtypeinfo.pets.*;
publicclassListFeatures...{
publicstaticvoidmain(String[]args)...{
Randomrand=newRandom(47);
List<Pet>pets=Pets.arrayList(7);
System.out.print("1:"+pets);
System.out.println();
Hamsterh=newHamster();
pets.add(h);
System.out.print("2:"+pets);
System.out.println();
System.out.print("3:"+pets.contains(h));
System.out.println();
pets.remove(h);
Petp=pets.get(2);
System.out.print("4:"+pets.indexOf(p));
System.out.println();
Petcymric=newCymric();
System.out.print("5:"+pets.indexOf(cymric));
System.out.println();
System.out.print("6:"+pets.remove(cymric));
System.out.println();
System.out.print("7:"+pets.remove(p));
System.out.println();
System.out.print("8:"+pets);
System.out.println();
pets.add(3,newMouse());
System.out.print("9:"+pets);
System.out.println();
List<Pet>sub=pets.subList(1,4);
System.out.print("sublist:"+sub);
System.out.println();
System.out.print("10:"+pets.containsAll(sub));
System.out.println();
Collections.sort(sub);
System.out.print("sortedsublist:"+sub);
System.out.println();
System.out.print("11:"+pets.containsAll(sub));
System.out.println();
Collections.shuffle(sub,rand);
System.out.print("shufflesublist:"+sub);
System.out.println();
System.out.print("12:"+pets.containsAll(sub));
System.out.println();
List<Pet>copy=newArrayList<Pet>(pets);
sub=Arrays.asList(pets.get(1),pets.get(4));
System.out.print("sub:"+sub);
System.out.println();
copy.retainAll(sub);
System.out.print("13:"+copy);
System.out.println();
copy=newArrayList<Pet>(pets);
copy.remove(2);
System.out.print("14:"+copy);
System.out.println();
copy.removeAll(sub);
System.out.print("15:"+copy);
System.out.println();
copy.set(1,newMouse());//Replaceanelement
System.out.print("16:"+copy);
System.out.println();
copy.addAll(2,sub);
System.out.print("17:"+copy);
System.out.println();
System.out.print("18:"+pets.isEmpty());
System.out.println();
pets.clear();
System.out.print("19:"+pets);
System.out.println();
System.out.print("20:"+pets.isEmpty());
System.out.println();
pets.addAll(Pets.arrayList(4));
System.out.print("21:"+pets);
System.out.println();
Object[]o=pets.toArray();
System.out.print("22:"+o[3]);
System.out.println();
Pet[]pa=pets.toArray(newPet[0]);
System.out.print("23:"+pa[3].id());
System.out.println();
}
}
<p>
Java编译的是字节码,跟C++相反,启动不够快,效率不够高,难以精确控制内存,但是优点是编程比C++容易,代码比较安全但是容易留下性能隐患,跨平台靠字节码在各个平台复制(一处编译到处调试) |
|