集合框架下边的接口(针对数据结构)
- collection
- map
collection
- list
- set
map (两个set接口)
- HashMap
- TreeMap
list
- ArrayList 长度可变的数组,在内存中分配连续的空间;
- LinkedList 链表的存储方式
set
- HashSet
- TreeSet
装箱和拆箱
有违安全性,不推荐使用;
set集合
set存储一组唯一的无序的对象;,set接口不存在get方法;
HashMap接口常用方法
collections:
- 静态类,不需要实例化;
- 工具类
map接口
存放数据 put
取值 foreach 和 迭代器;
遗留问题:
foreach循环
for(声明语句 : 表达式)
{
//代码句子
}
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
public class Test {
public static void main(String args[]){
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String [] names ={"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}
迭代器:不用担心遍历时会超出集合长度;
public class Test{
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("Hello");
list.add("World");
list.add("HAHAHAHA");
//第一种遍历方法使用foreach遍历List
for (String str : list) { //也可以改写for(int i=0;i<list.size();i++)这种形式
System.out.println(str);
}
//第二种遍历,把链表变为数组相关的内容进行遍历
String[] strArray=new String[list.size()];
list.toArray(strArray);
for(int i=0;i<strArray.length;i++) //这里也可以改写为 foreach(String str:strArray)这种形式
{
System.out.println(strArray[i]);
}
//第三种遍历 使用迭代器进行相关遍历
Iterator<String> ite=list.iterator();
while(ite.hasNext())//判断下一个元素之后有值
{
System.out.println(ite.next());
}
}
}
comparable
comparable接口 字符串比较大小
compareTo() 方法用于两种方式的比较:
字符串与对象进行比较。
按字典顺序比较两个字符串。
//字符串与对象比较
int compareTo(Object o)
//字符串与字符串比较
int compareTo(String anotherString)
this.title.compareTo(o.title)
比较器
使用比较器可以 让我们明确定义按照什么样的排序顺序;