JAVA学习日记----------容器总结

来源:互联网 发布:mac 换ssd 编辑:程序博客网 时间:2024/06/12 23:27

重点:1 3 6 9 

一. 

一张类图

collection: Set--->HashSet

    List--->LinkedList,ArrayList

Map:     HashMap


二.三个知识点:

1.迭代器

1). java.util.Iterator中有三个方法hasNext() next() remove()

2). foreach:使用增强for,必须实现java.lang.Iterable接口,重写iterator()方法

2.比较器

1).实体类可以排序,必须实现java.lang.Comparable重写compareTo()方法

2).排序比较器,必须实现java.util.Comparator重写compare()方法

另外:排序容器TreeSet, TreeMap

3.泛型:<>

泛型类,泛型接口,泛型擦除,通配符? extends super 泛型嵌套


三.六个接口

Collection 

Set

List

Map

Iterator

Comparable


四.九个常用类的使用

关注:添加,删除,修改,查看,遍历

1.ArrayList:实现是数组,便于查看多于修改

add()

remove()

set()

get()

for+get  foreach() Iterator

2.LinkedList:实现是用链表,便于修改多于查看

3.HashSet:要求元素要重写hashcode+equals方法

4.TreeSet元素可以排序或者提供排序的业务类

5.HashMap: 键不能重复,值可以重复,必须重写hashcode+equals方法

6.Properties:资源配置文件,相对路径获取路径

7.Hashtable:键与值都不能为null且线程安全

8.Stack:栈

9.Collections:工具类

以上是容器需要掌握的知识,具体需要看书了解,知识大纲


今天做的Topcoder题目如下:SRMS 308 DIV 2

Problem Statement

 

In a set of distinct numbers, the median is an element M such that the number of elements greater than M is equal to the number of elements smaller than M. For example, in a set {1, 4, 2, 5, 7} the median is 4 because two elements (5 and 7) are greater than 4 and 2 elements (1 and 2) smaller than 4. The set {1, 5, 8, 3} has no median because no element from it satisfies the definition above.

You are given a int[] numbers. Return the median of numbers or -1 ifnumbers has no median.

Definition

 Class:MedianOfNumbersMethod:findMedianParameters:int[]Returns:intMethod signature:int findMedian(int[] numbers)(be sure your method is public)代码如下:

import java.util.Arrays;

public class MedianOfNumbers {
public int findMedian(int[] numbers){
if(numbers.length%2!=0){
Arrays.sort(numbers);
int index = (numbers.length-1)/2;
return numbers[index];
}else{
return -1;
}
}

}

使用到的方法是java.util.Arrays包中的sort()方法,从小到大排序数组;

原创粉丝点击