java SE 学习笔记2

来源:互联网 发布:安卓看广告赚钱软件 编辑:程序博客网 时间:2024/06/01 10:33



.LOG


 // java SE 面向对象笔记2


1, 接口中的方法都是抽象方法,都是public的。
2, 接口中的成员变量都是public 、final 、static 的。
3, 一个类不能即是abstract又是final。(矛盾)
4, Design Pattern (设计模式): 23种经典模式。 
单例模式(Singleton):表示某个类只能生成一个对象并且自行创建这个对象而且自行向系统提供这个对象。    
//eg:singletontest

//第一种表达方式
public class SingletonTest
{
public static void main(String[] args)
{
Singleton singleton = Singleton.getInstance();
Singleton singleton2 = Singleton.getInstance();


System.out.println(singleton == singleton2);
}
}


class Singleton
{
private static Singleton singleton = new Singleton();

private Singleton()
{

}


public static Singleton getInstance()
{
return singleton;
}

}

//第二种表达方式
public class SingletonTest
{
public static void main(String[] args)
{
Singleton singleton = Singleton.getInstance();
Singleton singleton2 = Singleton.getInstance();


System.out.println(singleton == singleton2);
}
}


class Singleton
{
private static Singleton singleton;

private Singleton()
{

}


public static Singleton getInstance()
{
if(singleton == null)
{
singleton = new Singleton();
}


return singleton;
}

}


5, 包(package):用于将完成不同功能的类分门别类,放在不同的目录(包)。
1, 命名规则:将公司域名反转作为包名,每个字母都小写。
2, 包名中每一个点就是一层文件,类必须放在包执行。
3, 编译带有package声明的java源文件有两种方式:
a),直接手动建包,将类放在包中。
b),JVM 自动建包,javac —d . 包名 (.表示当前目录)
4, 导入包(import):import com.shengsiyuan.*;    *是通配符,表示该包下所有的类。
6, instanceof 关键字:判断某个对象是否是某个类的实例。(子类对象是父类的实例)
语法形式:引用名 instanceof 类名(接口名),返回一个 boolean 值。
7, 访问修饰符: public (公共的)      访问权限:同类,同包,子类,不同包。
protected (受保护的)访问权限:同类,同包,子类。
没有修饰符(默认的) 访问权限:同类,同包。
private (私有的)   访问权限:同类。
8, 相等性的比较:  "=="
1, 对于原生类型来说,比较的是值,对于引用类型来说,比较的是
    左右两边的引用是否指向同一个地址。
9, java.lang.object类:java.lang包 自动导入。
10, API(applicatiaon programing interface)
11, 当打印引用时,实际上会打印出引用所指对象的toString()方法的返回值,
因为每个类都直接或间接的继承自object。而object类中定义了toString().
12, 关于进制的表示:16进制,逢16进1, 0~9,A,B,C,D,E,F(不区分大小写) 
13, Object类中的equals()方法:java中的每个类都具有该方法,对与
      object类的equals()方法来说,它是判断调用equals()方法的引用与传进来
 的引用是否一致,即两个引用是否指向同一个对象。它和“==” 等价。 
14, 对于String类的equals()方法来说,它是判断当前字符串与传进来的字符串的内容是否一致。 
15, 对于String对象的相等性判断来说,请使用equals()方法,不要使用“==”。
16, 总结“==”和equals(): Object类中的equals()和“==”一样,比较的是地址,而像String类等继承Object类
  的类重写了equals()方法,则重写的方法比较的是字符串的内容。
  eg:equalsTest
  
   public class EqualsTest
{
public static void main(String[] args)
{
Student s1 = new Student("zhangsan");
Student s2 = new Student("zhangsan");


System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}


class Student
{
String name;


public Student(String name)
{
this.name = name;
}

public boolean equals(Object anObject)
{
if(this == anObject)
{
return true;
}


if(anObject instanceof Student)
{
Student student = (Student)anObject;

if(student.name.equals(this.name))
{
return true;
}
}


return false;
}


}


  
17, String 是常量,其对象创建后就无法改变,当使用+拼接字符串时,表示生成新的对象。   
18, String Pool(字符串池) ,
19, String s = "aaa";  (字面值赋值)     //和intern()方法原理一样
1,查找String Pool 中是否存在“aaa”这个对象,如果不存在,则在String Pool 
 中创建一个“aaa”对象,然后将String Pool 中的这个“aaa”对象的地址返回赋给引用变量 s ,
 这样 s 会指向String Pool 中的这个“aaa” 对象。
2, 如果存在,则不创建对象,直接将String Pool 中的这个“aaa”地址返回来,赋给 s 引用。
20, String string = new String();
1, 首先在String Pool 中查找有没有“aaa”这个字符串对象,如果有,则不在String Pool 中再去创建“aaa”
这个对象了,直接在堆中(heap)创建一个“aaa”对象,然后将堆中的这个“aaa”对象的地址返回来,
赋给 s 引用,导致 s 指向了堆中创建的这个“aaa”字符串对象。
2,  如果没有,则首先在 String Pool 中创创建一个“aaa”对象,
  然后再在堆中创建一个“aaa”对象,然后将堆中的这个“aaa”对象的地址返回来,赋给s引用个,导致
  s指向了堆中所创建的这个“aaa”对象。
21, StringBuffer :变量,可以调用append()方法追加内容(可以是字符串‘变量等)
22, 包装类(Wrapper Class):针对于原生数据类型的包装。所有的包装类(8个)都位于java.lang包中。
包装类的双向转换:eg:integerTest

public class IntegerTest
{
public static void main(String[] args)
{
/* String s = "aaaa";
int c = 10;
String str = String.valueOf(c);    //注释为String<-->int 
s1 = Integer.parseInt(s);
*/
int a = 10;

Integer integer = new Integer(a);

int b = integer.intValue();


System.out.println(a == b);
}
}

23, 数组(Array) :相同类型的数据的集合。(数组是对象)
数组在使用时必须遍历
a)原生数据类型:
1, 定义数组:type[] 变量名 = new type[数组的长度];  int[] a = new int[];
int[] a = {1,2,3,4};  int [] a = new int[]{1,2,3,4};
2, 数组中的 length 是 public   final .数组长度确定后不能改变大小。
3, 数组中元素的默认值为0;   数组中的equals()方法没有重写。
4, 数组名指向数组的首地址。
b) 对象数组:
1, 数组元素的初始值默认为空(引用),声明了对象数组则表示产生了几个引用变量(元素),
故每个元素必须重新开辟新空间( new 新对象)才能使用。    //eg:arraytest3

public class ArrayTest3
{
public static void main(String[] args)
{
Student[] s = new Student[100];

for(int i = 0; i < s.length; i++)
{
s[i] = new Student();


s[i].name = i % 2 == 0 ? "zhangsan" : "lisi";
}


for(int i = 0; i < s.length; i++)
{
System.out.println(s[i].name);
}
}
}


class Student
{
String name;
}

24, 二维数组:数组的数组。type[][] a = new type [长度][长度];  int[][] a = new int[][] {{1,2,3},{4},{5,4,}};

1, 不规则数组的声明,利用的是数组的数组,也就是说一维数组,默认表示行,可在行中为列开辟空间
反之则不成立。    //eg:arraytest5  


public class ArrayTest5
{
public static void main(String[] args)
{
/*
int[][] a = new int[3][];      


a[0] = new int[2];
a[1] = new int[3];
a[2] = new int[1];
*/


//int[][] a = new int[][3];

//int[] a = new int[]{1, 2 ,3};


int[][] a = new int[][]{ {1 ,2 ,3}, {4}, {5, 6, 7, 8} };


for(int i = 0; i < a.length; i++)
{
for(int j = 0; j < a[i].length; j++)
{
System.out.print(a[i][j] + " ");
}


System.out.println();
}
}
}



25, 在方法中实现两个数的交换:知识点:方法参数的传递
用数组(对象)传参实现,原生数据类型实参对形参没有影响。
26, 数组的比较 eg:arrayequalstest


import java.util.Arrays;


public class ArrayEqualsTest
{
// compare the content of two arrays
public static boolean isEquals(int[] a, int[] b)
{
if(a == null || b == null)
{
return false;
}


if(a.length != b.length)
{
return false;
}

for(int i = 0; i < a.length; i++)
{
if(a[i] != b[i])
{
return false;
}
}


return true;
}

public static void main(String[] args)
{
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};


System.out.println(isEquals(a, b));


System.out.println(Arrays.equals(a, b));
}
}




27, Arrays 类(数组的比较,查询,复制等等)
28, System 类 arraycopy()方法
29, 三维数组。type[][][] a = new type [3][2][5];


30, 排序

public class Sort 
{
public static void main(String[] args)
{
array[] arr = new array[]{4,5,2,9,14,23,0,7,45};

BubbleSort bubble = new BubbleSort();
bubble.bubbleS(arr);

SelectSort select = new SelectSort();
select.selectS(arr);
}
}


// Bubble : 第一个数与第二个数比较,如果大于后面的数就相互交换位置,以此类推。


class BubbleSort
{
public void bubbleS(array[] arr )
{
//走了多少趟
for(int i=0; i<arr.length-1; i++)
{
//每个数的比较次数
for(int j=0; j<arr.length-1-i; j++)
{
//比较并交换值
int tmpe = 0;

if(arr[j]>arr[j+1])
{
tmpe = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmpe;

}
}
}

for(int k=0; k<arr.length; k++ )
{
System.out.print(arr[k]+"  ");
}

}

}


// 从所有的数中找到最小值和第一个比较,如果小于就交换位置,然后将除比较出来的数之外的数做相同的操作。


class SelectSort
{
public void selectS(array[] arr )
{
//比较了多少趟
for(int i=0; i<arr.length-1;i++)
{
//选取最小数
int j=0;
int min =arr[j];

for(j=0; j<arr.length-1-i;j++)
{
if(min>arr[j+1])
{
min = arr[j+1];
}
}

//比较
if(min>arr[j])
{
int tmpe =0;

tmpe = arr[j];
arr[j] = min;
min = tmpe;
}
}

for(int k=0; k<arr.length; k++ )
{
System.out.print(arr[k]+"  ");
}
}


}

// 首先将数分为有序和无序的,有序组中起初只有一个数,然后将无序组中数依次插入有序组中。

class insertSort
{
public void insertS(int[] arr )
{
//除第一个数外的数中每次找出一个数与第一个数比较
for(int i = 1; i < arr.length; i++)
{
int j=i-1;
int temp=arr[i];

//比较
while(j >= 0 && temp < arr[j])
{
//第一个数后移,下标不变(将第一个数作为插入点)
arr[j+1] = arr[j];
j--;
}

arr[j+1] = temp; 

}

for(int k=0; k<arr.length; k++ )
{
System.out.print(arr[k]+"  ");
}

}

}

31, 查找
// BinaryFind     ( 前提是有序数组)
// 首先找到最中间的数,然后中间数和待查找的数比较,如果待查找的数在中间数左面,就在左面按照相同的方法查找,右面亦如此。


class BinaryFind
{
public void find(int leftindex , int rightindex ,int key ,int[] array)
{
//找到中间数
int midindex = (leftindex + rightindex)/2;
int midVal = array[midindex];

//比较
if(midVal == key)
{
System.out.println("该值为第" + midindex + "个数");
}else if (midVal > key)
{
find(leftindex , midindex-1, key , array);
}else if ( midVal < key )
{
find( midindex+1 , rightindex ,key ,array );
}else 
{
System.out.println("没有找到");
}

}

}


JDK 中产生的随机数都是大于等于左边小于右边,

//产生随机数的两种方法:
eg:randomtest

import java.util.Random;


public class RandomTest
{
public static void main(String[] args)
{
Random random = new Random();


for(int i = 0; i < 50; i++)
{
//System.out.println(random.nextInt(41) + 10);

double result = Math.random();


result *= 41;


int result2 = (int)result;


result2 += 10;


System.out.println(result2);


}
}
}


问题:产生50个正数,范围为[10,50], 统计每个数字出现的次数以及出现次数最多的数字和它的个数,
 最后将每个数字和出现的次数打印出来,没有出现的数字不打印,打印的数字按升序排列。
 
import java.util.Random;


public class RandomTest2
{
public static void main(String[] args)
{
int[] count = new int[41];

Random random = new Random();


for(int i = 0; i < 50; i++)
{
//产生随机数并记录每个数字出现的次数
int number = random.nextInt(41) + 10;    // [10, 50]


System.out.println(number);

count[number - 10]++;
}


//打印的数字出现的次数
for(int i = 0; i < count.length; i++)
{
if(0 == count[i])
{
continue;
}


System.out.println((10 + i) + "出现次数:" + count[i]);
}


//出现最大次数
int max = count[0];


for(int i = 0; i < count.length; i++)
{
if(max < count[i])
{
max = count[i];
}
}


System.out.println("出现的最大次数为:" +  max + "次");


//找到出现次数最多的数字
for(int i = 0; i < count.length; i++)
{
if(max == count[i])
{
System.out.println(i + 10);
}
}
}
}