`

ArrayList ,LinkedList, TreeSet的使用方法

    博客分类:
  • java
阅读更多
import java.util.*;

public class ArrayListTest_1 {
	public static void main(String[] args) {
		
		@SuppressWarnings("rawtypes")
		ArrayList<Comparable> al = new ArrayList<Comparable>();// 创建一个ArrarList对象
		System.out.println("a1 的初始化大小:" + al.size());
		// 向ArrayList对象中添加新内容
		al.add(Double.valueOf(Math.random()));
		al.add(Float.valueOf(14.2f));
		al.add("hellow");
		al.add("A");
		al.add("B");
		System.out.println("a1 的内容: " + al);                //集合类的输出方式!!!!!!
		al.add(1, "%");          // add()方法    目的是把%加在ArrayList对象的第2个位置
		System.out.println("a1 add后的大小:" + al.size());   //集合类的大小!!!!!!
		System.out.println("a1 add后的内容:" + al);
		// 从ArrayList中移除数据
		al.remove("Z");                                              //没有匹配的话为空操作
		al.remove(3);       //移除的下标为3的数据
		System.out.println("a1 remove后的大小: " + al.size());
		System.out.println("a1 remove后的内容: " + al);
	}
}

//*********************************************************************

import java.util.*;

public class LinkedListTest_1 {
	public static void main(String[] args) {
		Calendar rightNow = Calendar.getInstance();
		Date d = rightNow.getTime();
		LinkedList<String>ll = new LinkedList<String>();// 创建LinkedList对象
		// 加入元素到LinkedList中
		ll.add("now");
		ll.add("the");
		ll.add("time");
		ll.add("is");
		//ll.addFirst"1"+Integer.valueOf(123)+"");  // 在链表的第一个位置上加入int型数据
		ll.addLast(""+d+"");  // 在链表的最后个位置加上Date型数据
		ll.add(2, "*");  // 在链表第三个元素的位置上加入数据
		System.out.println("LinkedList 最初的内容:" + ll);
		ll.remove(0);  // 可以通过下标,从链表中移除元素
		ll.remove("*");  // 可以从链表中直接移除元素内容
		System.out.println("LinkedList remove后的内容:" + ll);
		Object obj = ll.get(3);  // 取得下标为3的元素值
		ll.add(0, obj + "right?"); // 将修修改后的新值插入到下标为0的位置上
		System.out.println("LinkedList 修改后的内容:" + ll);
	}
}


//****************************************************************************
import java.util.*;

public class TreeSetTest_1 {
	public static void main(String[] args) {
		TreeSet<String> ts = new TreeSet<String>(); // 创建一TreeSet对象 输入里面的东西是自然排序的 以第一个字符的sacii码
		// 加入元素到TreeSet中
		ts.add("A");
		ts.add("def");
		ts.add("ni");
		ts.add("o");
		ts.add("WORD");
		ts.add("Boy");
		System.out.println("TreeSet: " + ts);  //
		Iterator<String> it = ts.iterator();     // 创建迭代器it
		System.out.println("用Iterator迭代遍历:");
		while (it.hasNext()) {           // 判断是否还有元素可以进行迭代,有则返回true
			System.out.print(it.next() + "  ");// 返回下一个元素
		}
	}
}
//TreeSet: [A, Boy, WORD, def, ni, o]
//用Iterator迭代遍历:
//A  Boy  WORD  def  ni  o  
分享到:
评论

相关推荐

    Collectionjs:SortedSet,SortedList,Queue,ArrayList,LinkedList,TreeSet,HashMap

    它可以在 nodejs 和浏览器中使用。 var sets=new Collection.SortedSet(); sets.add('z'); sets.add('c'); sets.add('a'); sets.add('a'); sets.add('b'); sets.add('k'); console.log("Contains k "+sets....

    高级编程-java实验报告.docx

    实验目的及要求 ...1)Java集合框架中几种具体实现的使用:ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue; 2)HashMap和TreeMap的使用; 3)枚举类型的使用,EnumSet和EnumMap的使用;

    day016-list和set笔记以及代码.zip

    如果是定制排序,需要创建TreeSet对象的时候,传入一个Comparetor接口实现类对象,重写compare方法 一般是默认排序用自然排序(Comparable接口),特殊排序用定制排序(Comparetor接口实现) LinkedHashSet:如果...

    JAVA容器讲解.pdf

    Java容器讲解PPT,Collection Map(HashMap TreeMap LinkedHashMap) List (ArrayList LinkedList Vector) Set (HashSet TreeSet LinkedHashSet)

    Collections源码java-Java_collections:Java的ArrayList、LinkedList、HashMap、Tr

    Collections 源码 java Java Java的ArrayList、LinkedList、HashMap、TreeMap、LinkedHashMap、HashSet、TreeSet相关源码分析,及相关问题和应用总结。

    CIS-263-project-1

    值:一组单词(LinkedList,ArrayList,TreeSet) 伪码 For each word in the dictionary Calculate the key Check if the key is already present in the map If it is not present in the map Instantiate a ...

    Java 基础核心总结 +经典算法大全.rar

    ArrayList Vector LinkedList 类Stack HashSet TreeSet LinkedHashSet 类 PriorityQueue HashMap TreeMap 类 LinkedHashMap 类 Hashtable 类IdentityHashMap 类WeakHashMap 类 Collections 类集合实现类特征图 泛形 ...

    Java集合框架完整说明便于了解集合

    java集合在日常开发中经常用到,对基础的掌握尤其重要,其中List,Set,Map的作用以及使用的场景和分类描述,其中Arraylist 与 LinkedList 区别,HashSet与TreeSet与LinkedHashSet对⽐,LinkedHashMap和HashMap,...

    Java 集合方面的面试题

    ArrayList 和 LinkedList 有什么区别? HashSet 和 TreeSet 有什么区别? HashMap 和 TreeMap 有什么区别? 什么是迭代器?如何使用它来遍历集合? 什么是 fail-fast 机制? 如何使用 Collections 类对集合进行排序...

    java集合类原理面试题

    ArrayList和LinkedList有什么区别 有哪些线程安全的List 介绍一下ArrayList的数据结构 谈谈CopyOnWriteArrayList的原理 说一说TreeSet和HashSet的区别 说一说HashSet的底层结构 BlockingQueue是怎么实现的 Stream...

    实验05 Java集合.doc

    3)了解List接口及主要实现类(ArrayList、LinkedList、Vector) 4)了解Map接口及主要实现类(HashMap、TreeMap、HashTable) 二、实验内容及步骤 1、编写程序练习将以下5个Person类的对象放在一个HashSet中。 姓名...

    对java基础集合部分(List、HashMap、HashSet、ArrayList等)底层源码的分析与总结

    这篇集合总结一共包括十二节,介绍了一些接口和实现类的底层源码以及基本的增加、删除元素等的操作(包括List、Map、Set接口、ArrayList、Vector、LinkedList、HashSet、TreeSet、HashMap、TreeMap等实现类)。...

    Java集合多线程安全.docx

    LinkedList HashMap HashSet TreeMap TreeSet StringBulider 线程安全集合: Vector HashTable Properties 集合线程安全与解决方案 ArrayList线程安全问题 package ...

    javaee笔试题-JavaExercise:Java学习的代码,包括设计模式、se、ee以及相关测试代码

    LinkedList Set HashSet TreeSet Map HashMap 泛型 Iterator 线程 反射 虚拟机 Java8 新特性 Java EE 源码 JUnit SpringSide Jive 论坛 框架 J2EE Development without EJB SpringMVC 视频 看透SpringMVC Spring ...

    集合笔记整理.doc

    集合笔记 list arraylist集合 linkedlist集合 set hashset集合 treeset集合 map hashmap集合 terrset集合 不可变集合

    Java知识集.docx

    Java知识集是Java编程语言的核心概念和技术,涵盖了Java编程语言的基础知识、面向对象编程、...集合类:包括ArrayList、LinkedList、HashSet、TreeSet等集合类的使用。 反射机制:包括Class、Method、Field等类的使用。

    Java实验报告

    正确使用字符串相关类(String,StringBuffer,StringTokenizer),日期类(Date、Calendar);另外,还有ArrayList、LinkedList、HashTable、TreeSet等都涉及在内。用Java实现对数据的增、删、改、查功能。

    java8源码-JavaRobot:Java学习笔记,JavaLearningNote

    LinkedList CopyOnWriteArrayList Vector Map源码系列 HashMap LinkedHashMap ConcurrentHashMap TreeMap Hashtable Set源码系列 HashSet LinkedHashSet TreeSet HashSet Concurrent源码系列 待完善 JVM(Java虚拟机)...

    实验七:Java集合与泛型

    本次实验掌握了集合的概念、体系结构、分类及使用场景,了解了Set接口及主要实现类(HashSet、TreeSet),了解了解List接口及主要实现类(ArrayList、LinkedList、Vector),掌握ArrayList的使用及其与Vector的区别,...

    Java面试题,冲冲冲!.rar

    - 常见实现类有ArrayList、LinkedList和Vector等。 2. Set(集合): - 不允许重复元素。 - 不维护元素的插入顺序,元素存储的顺序可能发生变化。 - 提供了高效的查找和去重功能。 - 常见实现类有HashSet、TreeSet和...

Global site tag (gtag.js) - Google Analytics