一些学习中遇到的问题整理
2016-08-25 22:10
441 查看
附上自己学习过程中的思维导图:http://pan.baidu.com/s/1c2FS74W
1.数组打印
int []num = {1,2,3,4,5};
System.out.println(num.toString());
//输出:[I@15db9742
System.out.println(Arrays.toString(num));
//输出:[1, 2, 3, 4, 5]
int [][]nums = {{1,2},{3,4,5},{6,7}};
System.out.println(Arrays.toString(nums));
//输出:[[I@6d06d69c, [I@7852e922, [I@4e25154f]
System.out.println(Arrays.deepToString(nums));
//输出:[[1, 2], [3, 4, 5], [6, 7]]
2.冒泡排序
for (int i = 0;i <
nums.length-1;
i++) {
for (int j = 0;j <
nums.length-1-i;j++) {
if(nums[j]>nums[j+1]){
int temp =nums[j];
nums[j] =nums[j+1];
nums[j+1] =temp;
}
}
}
3. 选择排序
for(int i=0;i<nums.length-1;i++){
int index=i;
for(int j=i;j<nums.length;j++){
if(nums[index]>nums[j]){
index=j;//保存的当次最小的索引下标
}
}
if(index!=i){
int temp=nums[index];
nums[index]=nums[i];
nums[i]=temp;
}
}
4.可变长参数:
1.可变参数:方法参数部分指定类型的参数个数是可变多个
2.声明方式:方法名(参数的类型名...参数名)
3.可变参数方法的使用与方法参数部分使用数组是一致的
4.方法的参数部分有可变形参,需要放在形参声明的最后
public static int add(String name,int... nums){};
5.多态:
创建对象时,一般使用父类类型 对象名=new 子类类型();
编译时,看等号左边的类型
运行时,看等号右边的类型
6.static:
static只能修饰成员变量,不能修饰局部变量。
static修饰代码块 只在类加载时,执行一次,在调用构造函数之前被执行
7.Collection集合的基本特征:
<2、 Set接口
Map也是接口,但没有继承Collection接口。该接口描述了从不重复的键到值的映射。Map接口用于维护键/值对(key/value
pairs)。
特征:它描述了从不重复的键到值的映射。
两个重要的实现类:HashMap和TreeMap
1.HashMap,中文叫散列表,基于哈希表实现,特点就是键值对的映射关系。一个key对应一个Value。HashMap中元素的排列顺序是不固定的。更加适合于对元素进行插入、删除和定位。允许使用null键和null值,与HashSet一样,不保证映射的顺序。
2.TreeMap,基于红黑书实现。TreeMap中的元素保持着某种固定的顺序。更加适合于对元素的顺序遍历。若使用自定义类作为TreeMap的key,所属类需要重写equals()和hashCode()方法。
所有实现了Collection接口的容器类都有iterator方法,用于返回一个实现了Iterator接口的对象。Iterator对象称作迭代器,Iterator接口方法能以迭代方式逐个访问集合中各个元素,并可以从Collection中除去适当的元素。
1. Collection collection = new ArrayList;
2. <span style="font-size:10px;">Iterator it = collection.iterator(); // 获得一个迭代子
3. while(it.hasNext())
4. {
5. Object obj = it.next(); // 得到下一个元素
6. }
8.集合中涉及到比较时需要重写的方法:
ArrayList ->对象equals方法(eclipse可直接导入)
HashSet (HashMap) ->equals和hashCode(eclipse可直接导入)
@Override
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(obj instanceof Student){
Student stu = (Student)obj;
if(this.getName().equals(stu.getName())&&this.getAge()==stu.getAge())
return true;
}
return super.equals(obj);
}
@Override
public int hashCode() {
return name.hashCode()+new Integer(age).hashCode();
}
TreeSet(TreeMap)(自然排序和定制排序)->自然排序:空的构造器,让实体实现java.lang.Comparable接口重写 public int compareTo(Object obj)方法,让对象具有比较的能力
public class Student2 implements Comparable{
@Override
public int compareTo(Object o) {
if(o instanceof Student2){
Student2 stu = (Student2)o;
if(getAge()-stu.getAge()>0){
return 1;
}else if(getAge() == stu.getAge()){
return getName().compareTo(stu.getName());
//return 0;
}else{
return -1;
}
}
return -1;
}
}
定制排序: 带Comparator对象的构造函数,自定义比较器类实现Comparator接口 public int compare(Object o1,Objeact o2)
import java.util.Comparator;
public class StudentComparator implements
Comparator {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Student2 && o2 instanceof Student2) {
Student2 stu1 = (Student2) o1;
Student2 stu2 = (Student2) o2;
if (stu1.getAge() > stu2.getAge()) {
return 1;
} else if (stu1.getAge() == stu2.getAge()) {
return stu1.getName().compareTo(stu2.getName());
} else {
return -1;
}
}else{
throw new RuntimeException("类型不匹配");
}
}
}
9.Math.round(11.5)等於多少? Math.round(-11.5)等於多少?
Math 类中提供了三个与取整有关的方法: ceil、 floor、 round,这些方法的作用与它们的英文名称的含义相对应,例如,ceil
的英文意义是天花板,该方法就表示向上取整,Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的结果是-11;floor
的英文意义是地板,该方法就表示向下取整, Math.ceil(11.6)的结果为11,Math.ceil(-11.6)的结果是-12;最难掌握的是round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,
Math.round(11.5)的结果为12, Math.round(-11.5)的结果为-11。
10.
1.数组打印
int []num = {1,2,3,4,5};
System.out.println(num.toString());
//输出:[I@15db9742
System.out.println(Arrays.toString(num));
//输出:[1, 2, 3, 4, 5]
int [][]nums = {{1,2},{3,4,5},{6,7}};
System.out.println(Arrays.toString(nums));
//输出:[[I@6d06d69c, [I@7852e922, [I@4e25154f]
System.out.println(Arrays.deepToString(nums));
//输出:[[1, 2], [3, 4, 5], [6, 7]]
2.冒泡排序
for (int i = 0;i <
nums.length-1;
i++) {
for (int j = 0;j <
nums.length-1-i;j++) {
if(nums[j]>nums[j+1]){
int temp =nums[j];
nums[j] =nums[j+1];
nums[j+1] =temp;
}
}
}
3. 选择排序
for(int i=0;i<nums.length-1;i++){
int index=i;
for(int j=i;j<nums.length;j++){
if(nums[index]>nums[j]){
index=j;//保存的当次最小的索引下标
}
}
if(index!=i){
int temp=nums[index];
nums[index]=nums[i];
nums[i]=temp;
}
}
4.可变长参数:
1.可变参数:方法参数部分指定类型的参数个数是可变多个
2.声明方式:方法名(参数的类型名...参数名)
3.可变参数方法的使用与方法参数部分使用数组是一致的
4.方法的参数部分有可变形参,需要放在形参声明的最后
public static int add(String name,int... nums){};
5.多态:
创建对象时,一般使用父类类型 对象名=new 子类类型();
编译时,看等号左边的类型
运行时,看等号右边的类型
6.static:
static只能修饰成员变量,不能修饰局部变量。
static修饰代码块 只在类加载时,执行一次,在调用构造函数之前被执行
7.Collection集合的基本特征:
<1、Collection接口
Collection是最基本集合接口,它定义了一组允许重复的对象。Collection接口派生了两个子接口Set和List,分别定义了两种不同的存储方式,如下:<2、 Set接口
Set接口继承于Collection接口,它没有提供额外的方法,但实现了Set接口的集合类中的元素是无序且不可重复。
特征:无序且不可重复。
Set接口的实现类常用的有:HashSet,LinkedHashSet,TreeSet.
HashSet具有以下特点:1.不能保证元素的排列顺序2.HashSet不是线程安全的3.集合元素可以是null。
LinkedHashSet是 HashSet的子类
TreeSet 两种排序方法:自然排序和定制排序。默认情况下,TreeSet采用自然排序。
自然排序:如果试图把一个对象添加到
TreeSet 时,则该对象的类必须实现 Comparable接口,并重写父类的compareTo方法。
定制排序:可通过Comparator接口的帮助。需要重写compare(T o1,T o2)方法。
<3、 List接口
List接口同样也继承于Collection接口,但是与Set接口恰恰相反,List接口的集合类中的元素是对象有序且可重复。
特征:有序且可重复。
两个重要的实现类:ArrayList和LinkedList
1.ArrayList特点是有序可重复的
2.LinkedList是一个双向链表结构的。
List接口的实现类常用的有:ArrayList、LinkedList和Vector。
在各种list中,最好把ArrayList作为缺省选择。当插入、删除频繁时,使用LinkedList;Vector总是比ArrayList慢,所以即使Vector是线程安全的,也应尽量避免使用。
<4、Map接口
Map也是接口,但没有继承Collection接口。该接口描述了从不重复的键到值的映射。Map接口用于维护键/值对(key/valuepairs)。
特征:它描述了从不重复的键到值的映射。
两个重要的实现类:HashMap和TreeMap
1.HashMap,中文叫散列表,基于哈希表实现,特点就是键值对的映射关系。一个key对应一个Value。HashMap中元素的排列顺序是不固定的。更加适合于对元素进行插入、删除和定位。允许使用null键和null值,与HashSet一样,不保证映射的顺序。
2.TreeMap,基于红黑书实现。TreeMap中的元素保持着某种固定的顺序。更加适合于对元素的顺序遍历。若使用自定义类作为TreeMap的key,所属类需要重写equals()和hashCode()方法。
<5、Iterator接口
Iterator接口,在C#里有例外一种说法IEnumerator,他们都是集合访问器,用于循环访问集合中的对象。所有实现了Collection接口的容器类都有iterator方法,用于返回一个实现了Iterator接口的对象。Iterator对象称作迭代器,Iterator接口方法能以迭代方式逐个访问集合中各个元素,并可以从Collection中除去适当的元素。
1. Collection collection = new ArrayList;
2. <span style="font-size:10px;">Iterator it = collection.iterator(); // 获得一个迭代子
3. while(it.hasNext())
4. {
5. Object obj = it.next(); // 得到下一个元素
6. }
<6、Comparable接口
Comparable可以用于比较的实现,实现了Comparable接口的类可以通过实现comparaTo方法从而确定该类对象的排序方式。8.集合中涉及到比较时需要重写的方法:
ArrayList ->对象equals方法(eclipse可直接导入)
HashSet (HashMap) ->equals和hashCode(eclipse可直接导入)
@Override
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(obj instanceof Student){
Student stu = (Student)obj;
if(this.getName().equals(stu.getName())&&this.getAge()==stu.getAge())
return true;
}
return super.equals(obj);
}
@Override
public int hashCode() {
return name.hashCode()+new Integer(age).hashCode();
}
TreeSet(TreeMap)(自然排序和定制排序)->自然排序:空的构造器,让实体实现java.lang.Comparable接口重写 public int compareTo(Object obj)方法,让对象具有比较的能力
public class Student2 implements Comparable{
@Override
public int compareTo(Object o) {
if(o instanceof Student2){
Student2 stu = (Student2)o;
if(getAge()-stu.getAge()>0){
return 1;
}else if(getAge() == stu.getAge()){
return getName().compareTo(stu.getName());
//return 0;
}else{
return -1;
}
}
return -1;
}
}
定制排序: 带Comparator对象的构造函数,自定义比较器类实现Comparator接口 public int compare(Object o1,Objeact o2)
import java.util.Comparator;
public class StudentComparator implements
Comparator {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Student2 && o2 instanceof Student2) {
Student2 stu1 = (Student2) o1;
Student2 stu2 = (Student2) o2;
if (stu1.getAge() > stu2.getAge()) {
return 1;
} else if (stu1.getAge() == stu2.getAge()) {
return stu1.getName().compareTo(stu2.getName());
} else {
return -1;
}
}else{
throw new RuntimeException("类型不匹配");
}
}
}
9.Math.round(11.5)等於多少? Math.round(-11.5)等於多少?
Math 类中提供了三个与取整有关的方法: ceil、 floor、 round,这些方法的作用与它们的英文名称的含义相对应,例如,ceil
的英文意义是天花板,该方法就表示向上取整,Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的结果是-11;floor
的英文意义是地板,该方法就表示向下取整, Math.ceil(11.6)的结果为11,Math.ceil(-11.6)的结果是-12;最难掌握的是round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,
Math.round(11.5)的结果为12, Math.round(-11.5)的结果为-11。
10.
相关文章推荐
- 近期网友遇到的一些问题,整理出来 大家可以学习 交流 MySQL问题及解答
- 通过plsql 测试存储过程遇到的问题和学习到的一些基础知识整理
- 通过IIS发布网站遇到的一些问题整理
- 在学习SSH中遇到的一些问题与大家共享
- (转)WTL学习过程中遇到的一些问题总结
- 学习Hadoop中遇到的一些问题
- ISA的学习体会二:我做ISA服务器遇到的一些问题
- 【XEN学习笔记】XEN 4.1.0 PVOPS设置和启动中遇到的一些问题
- 学习shell 时遇到的一些问题(unexpected operator) 和解决方法
- 整理一下遇到过的一些问题
- 黑马程序员 + 在学习中遇到的一些问题2
- 整理一下安装Ubuntu以来遇到的一些问题(一)
- 新手常遇到的一些jquery问题整理
- 易语言学习中遇到的一些问题
- 学习JAVA语言过程中遇到了一些问题
- 新手常遇到的一些jquery问题整理
- 学习AJAX过程中遇到的一些问题
- grails 学习过程中遇到的一些问题
- grails 学习过程中遇到的一些问题