成都创新互联网站制作重庆分公司

java中关于集合的代码 java集合类详解和使用

对于集合类的操作的java代码实现的方法

//我用集合框架的老大跟子类ArrayList给你写个例子

宾县ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

import java.util.*;

public class CollectionDemo{

public static void main(String[] args){

Collection c = new ArrayList();

//增加

c.add("Java01");

c.add("Java02");

//删除0角标的值

c.remove(0);

//排序  为了方便,我把集合框架转成了String[]数组

String[] arr = new String[c.size()];

arr = c.toArray();

Arrays.sort(arr);

//最大值

System.out.println(arr[arr.length-1]);

//因为我是String字符串集合值,没办法比较平均值,可以更改为Interger

}

}

java集合有哪些

集合类型主要有3种:set(集)、list(列表)和map(映射)。

1、List(有序、可重复)

List里存放的对象是有序的,同时也是可以重复的,List关注的是索引,拥有一系列和索引相关的方法,查询速度快。因为往list集合里插入或删除数据时,会伴随着后面数据的移动,所有插入删除数据速度慢。

2、Set(无序、不能重复)

Set里存放的对象是无序,不能重复的,集合中的对象不按特定的方式排序,只是简单地把对象加入集合中。

3、Map(键值对、键唯一、值不唯一)

Map集合中存储的是键值对,键不能重复,值可以重复。根据键得到值,对map集合遍历时先得到键的set集合,对set集合进行遍历,得到相应的值。

扩展资料:

JAVA集合类型四种常见输出方式:

1、Iterator:迭代输出,是使用最多的输出方式。

2、ListIterator:是Iterator的子接口,专门用于输出List中的内容。

3、foreach输出:JDK1.5之后提供的新功能,可以输出数组或集合。

4、for循环。

代码示例如下:

for的形式:for(int i=0;iarr.size();i++){...}

foreach的形式: for(int i:arr){...}

iterator的形式:

Iterator it = arr.iterator();

while(it.hasNext()){ object o =it.next(); ...}

参考资料来源:百度百科:java集合类

删除JAVA集合中元素的实现代码

经常我们要删除集合中的某些元素 有些可能会这么写

复制代码 代码如下: public void operate(List list){ for (Iterator it = list iterator(); it hasNext();) { String str = (String)it next(); if (str equals("chengang")){ list remove(str); } } }

这种写法一运行就会报如下异常

Exception in thread "main" java util ConcurrentModificationException at java util AbstractList$Itr checkForComodification(AbstractList java: )

因为list在循环中的时候是不可以删除它的元素的 后来我是这样做的 一种很笨的方法 思路是这样的 创建一个List专门存放要被删除的元素 循环过后 用List removeAll方法把元素删除 代码如下

复制代码 代码如下: public void operate(List list){ List removeList= new ArrayList(); for (Iterator it = list iterator(); it hasNext();) { String str = (String)it next(); if (str equals("chengang")){ removeList add(str); } } list removeAll(removeList); }

这样也确实可以解决问题了 但是方法实在太笨重 其实可以有更简单的更高效的方法 就是用Iterator remove方法 如下

复制代码 代码如下: for (Iterator it = list iterator(); it hasNext();) { String str = (String)it next(); if (str equals("chengang")){ it remove(); } }   lishixinzhi/Article/program/Java/JSP/201311/19832

java集合中contain方法怎么写

java集合中的contain方法用来判断是否存在某个元素,编写的代码如下:

import java.util.HashSet;

class Dog{

String color;

public Dog(String s){

color = s;

}

}

public class SetAndHashCode {

public static void main(String[] args) {

HashSetDog dogSet = new HashSetDog();//新建一个set集合

dogSet.add(new Dog("white"));//添加元素

dogSet.add(new Dog("white"));

System.out.println("We have " + dogSet.size() + " white dogs!");

if(dogSet.contains(new Dog("white"))){//contain判断是否存在某个元素

System.out.println("We have a white dog!");

}else{

System.out.println("No white dog!");

}

}

}

求java代码。关于将一个集合按照要求分解成两个集合

//Platform.java

public class Platform {

/**

* @author lusong

*/

private String brandName;

private String model;

public Platform(String bString,String mString) {

brandName=bString;

model=mString;

}

public String getBrandName() {

return brandName;

}

public String getModel(){

return model;

}

}

//ShiXian.java,其中的getList()方法返回你要求的东东

//不知道合不合你的胃口

import java.util.*;

public class ShiXian {

ArrayListPlatform PlatformList=new ArrayListPlatform();

//用于包含型号的名称,用于验证重复,在getLIst方法中使用,因为内置类不能访问方法的局部变量所有在此定义方法中的变量

ListString arrayList=new ArrayListString();

public ShiXian() {

//向该集合中添加成员,型号之间用逗号隔开

PlatformList.add(new Platform("诺基亚","7210,5530,2120"));

PlatformList.add(new Platform("摩托罗拉","V3,L6"));

PlatformList.add(new Platform("诺基亚","N70,N73,N95"));

PlatformList.add(new Platform("摩托罗拉","V4,L7"));

PlatformList.add(new Platform("摩托罗","V4,L7"));

PlatformList.add(new Platform("摩","V4,L7"));

PlatformList.add(new Platform("摩","V4,L7"));

//你可以随意添加成员,注意型号之间用逗号隔开

}

//你的要求是返回一个包含所有品牌的List,每个List项为一个包含型号的List

public ListArrayListString getList(){

//用于最终返回结果的List

ListArrayListString list=new ArrayListArrayListString(){

//重写toString()方法,便于显示手机名称

@Override

public String toString() {

int i=0;

// TODO Auto-generated method stub

StringBuilder stringBuilder=new StringBuilder();

for (Iterator iterator = this.iterator(); iterator.hasNext();) {

ArrayListString arrayList1 = (ArrayListString) iterator.next();

stringBuilder.append(arrayList.get(i));

stringBuilder.append("的型号为:");

i=i+1;

stringBuilder.append(arrayList1.toString());

}

return stringBuilder.toString();

}

};

for (Iterator iterator = PlatformList.iterator(); iterator.hasNext();) {

Platform platform=(Platform)iterator.next();

if (arrayList.contains(platform.getBrandName())) {

//该手机已经存在,只用添加型号,并且验证是否重复

String[] strings=platform.getModel().split(",");

ArrayListString arrayList3=list.get(arrayList.indexOf(platform.getBrandName()));

for (int i = 0; i strings.length; i++) {

if (!arrayList3.contains(strings[i])) {

arrayList3.add(strings[i]);

}

}

}

else {

//该手机还不存在,要添加该手机,并添加型号

arrayList.add(platform.getBrandName());

ArrayListString arrayList3=new ArrayListString();

String[] strings=platform.getModel().split(",");

for (int i = 0; i strings.length; i++) {

arrayList3.add(strings[i]);

}

list.add(arrayList3);

}

}

return list;

}

public static void main(String[] args) {

System.out.println(new ShiXian().getList());

}

}


新闻名称:java中关于集合的代码 java集合类详解和使用
地址分享:http://cxhlcq.com/article/hgpdhs.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部