一、Integer,String
1) == 和 equals的用法 == 比较的是地址,equals比较的是内容
public static void main(String args[]){
Integer s1 = new Integer(5);
Integer s2 = new Integer(5);
System.out.println(s1==s2); //false
System.out.println(s1.equals(s2)); //true
Integer s3 = 5;
Integer s4 = 5;
System.out.println(s3==s4); //true
System.out.println(s3.equals(s4)); //true
//为什么s3==s4 因为Integer有一个缓存机制(-128-127)数字在这个范围,是直接被缓存进去的,以
//指向的地址是相同的
String st1="hello";
String st2="hello";
System.out.println(st1==st2);//true
//为什么st1==st2,因为String有一个pool机制,也就是说,首先会去缓存中找这个‘hello’,如果没 //有就加入到缓存中,有则直接取出,所以st2会直接取出st1的地址值
}
2、arrayList
arrayList的删除方法解读
String aa[]={"1","2","3","4"};
int size = aa.length;
int numMoved = size - 0 - 1;
if (numMoved > 0)
System.arraycopy(aa,1, aa, 0, numMoved);
aa[--size] = null;
for(String a : aa){
System.out.println(a);
}
删除数组的第一个元素,下标为0
3、arrays的并归算法
public class ZZ {
public int[] sort(int[] arrs){
if(arrs.length < 2){
return arrs;
}
int middle = arrs.length % 2 == 0 ? arrs.length >>> 1 : (arrs.length - 1) >>> 1;
int[] left = Arrays.copyOfRange(arrs, 0, middle);
int[] right = Arrays.copyOfRange(arrs, middle, arrs.length);
int[] lres = sort(left);
int[] rres = sort(right);
return merge(lres, rres);
}
private int[] merge(int[] lres, int[] rres) {
int[] res = new int[lres.length + rres.length];
int l = 0;
int r = 0;
int c = 0;
while(l < lres.length && r < rres.length){
if(lres[l] < rres[r]){
res[c++] = lres[l++];
} else {
res[c++] = rres[r++];
}
}
if(l == lres.length){
while(r < rres.length){
res[c++] = rres[r++];
}
return res;
}
if(r == rres.length){
while(l < lres.length){
res[c++] = lres[l++];
}
return res;
}
return res;
}
public static void main(String[] args) {
int [] aa={2,4,1,3,6,5,7,9,8};
ZZ zz = new ZZ();
int [] zzz = zz.sort(aa);
for(int a : zzz){
System.out.println(a);
}
}
}
4、构造器
public class Fa {
public Fa(){
aa();
}
public void aa(){
System.out.println("aaa");
}
}
public class Son extends Fa{
int b = 1;
public void aa(){
System.out.println("bbb"+b);
}
public static void main(String[] args) {
Son son = new Son();
son.aa();
}
}
输出的结果为:
bbb0
bbb1
并没有输出aaa,是因为子类的方法覆盖掉父类的方法,那么为什么b的值会为0和1呢,因为在父类中并没有执行到b赋值这一步
5、变量的初始化
1)public class test{
int a;
static int b;
{
int c;
}
static{
int e;
int f;
}
public test(){
System.out.println(1);
}
//先加载静态b,e,f,之后是a,c,最后是构造器
}
2)public class test{
int a;
static int b;
{
int c;
}
static{
int e;
b=1;
}
public test(){
System.out.println(1);
}
//先加载静态b,之后去复制b,在去加载e,之后是a,c,最后是构造器
}
3)public class test{
int a;
{
int c;
}
static{
int e;
b=1;
}
static int b;
public test(){
System.out.println(1);
}
//先加载静态b,之后去复制b,在去加载e,之后是a,c,最后是构造器
}
4)public class test1 extends test{
int a;
static int b;
{
int c;
}
static{
int e;
b=1;
}
public test1(){
System.out.println(1);
}
public static void main(String [] agrs){
new test1();
//此时会先执行父类的静态(test),之后执行子类的静态(test1),之后执行父类的非静态,子类的非静态
}
}
转载请注明原文地址: https://ju.6miu.com/read-1888.html