网站首页 > 文章中心 > 其它

集合java编程代码

作者:小编 更新时间:2023-10-19 13:05:08 浏览量:485人看过

用java编写程序,求集合的并集、交集和差集

public?static?void?main(String[]?args)?{

ListInteger?listA?=?Arrays.asList(A);

ListInteger?listB?=?Arrays.asList(B);

ListInteger?jiaoji?=?new?ArrayListInteger();

for(Integer?a:listA){

if(listB.contains(a)){

jiaoji.add(a);

集合java编程代码-图1

}

System.out.println(jiaoji);

ListInteger?bingji?=?new?ArrayListInteger();

if(!bingji.contains(a)){

bingji.add(a);

for(Integer?b:listB){

if(!bingji.contains(b)){

集合java编程代码-图2

bingji.add(b);

System.out.println(bingji);

ListInteger?chaji?=?new?ArrayListInteger();

if(!listB.contains(a)){

chaji.add(a);

System.out.println(chaji);

JAVA编程创建一个List集合cards,再54张扑克牌存放到其中,请按下列要求编写程序:

public class PlayCard {

private ListInteger list;

/**

*/

public PlayCard() {

list = new ArrayListInteger();

list.add(i);

public void showPlayCard(){

System.out.println("方式一");

for(Integer i : list){

System.out.print(i+"\t");

System.out.println("方式二");

for(int i=0;ilist.size();i++){

System.out.print(list.get(i)+"\t");

System.out.println("方式三");

Iterator Integer it = list.iterator();

while(it.hasNext()){

System.out.print(it.next()+"\t");

public void randomPlayCards(){

// 把牌打乱

Collections.shuffle(list);

public static void main(String[] args) {

PlayCard p = new PlayCard();

p.randomPlayCards();

p.showPlayCard();

用java编写程序,集合元素为小写字母,实现集合的交,并,补,差运算

SetCharacter result = new HashSetCharacter();

SetCharacter set1 = new HashSetCharacter() {

{

add('a');

add('b');

add('c');

集合java编程代码-图3

add('d');

add('e');

};

result.clear();

result.addAll(set1);

System.out.println("交集:" + result);

System.out.println("差集:" + result);

System.out.println("并集:" + result);

System.out.println("补集:"+result);

}else{

System.out.println("无补集");

以上就是土嘎嘎小编为大家整理的集合java编程代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章