![]()
![]()
![]()
数组也是类:
java类型有8种几本类型和引用类型,数组属于引用类型,数组也是对象。(通过new建出来的东西都在堆里面,int类型是4个字节),数组元素的初始值都为0,数组元素相当于对象的成员变量,
int[] a;
int b[];
//创建数组对象
a = new int[4];
b = new int[5];
//初始化(对数组元素的初始化)
//默认初始化:数组元素相当于对象的成员变量,默认值跟成员变量的规则一样。数字0,布尔false,char\u0000,引用:null
//动态初始化:
for(int i=0;i<a.length;i++){
a[i] = i*12;
}
//静态初始化
int c[] = {23,43,56,78}; //长度:4,索引范围:[0,3]
Car[] cars = {//数组里面存的是一个个对象的地址,数组对象和数组里面的对象元素在堆中是分开放的。
new Car("奔驰"),//"奔驰"这个常量是在堆中方法区中的类信息区中的常量池中。
new Car("比亚迪"),
new Car("宝马")
};
Car c2 = new Car("奔驰");//每次new都会在堆中新建一个对象,
System.out.println(c2==cars[0]);//false
string类:equals比较值是否相等,=比较地址是否相等。
String str = new String("abcd");
String str2 = new String("abcd");//“abcd”也存在于类的常量池中,
System.out.println(str2.equals(str)); //str和str2在栈中,指向不同的对象,true
System.out.println(str2==str);//false
System.out.println(str.charAt(2));
String str3 = "def";//存在常量池中
String str4 = "def";//共用上面那个常量池
System.out.println(str3.equals(str4));//true,str3和str4在栈中指向同一个常量池
System.out.println(str3==str4);//true
final变量的值不能被修改,只有一个值,
String gh = new String("a");//栈中gh的值(地址值)可以改,这个"a"字符串对象位于堆,他的属性private final char value[],这个char数组是不能改的,所以"a"这个对象不能改,
for (int i = 0; i < 1000; i++) {
gh = gh + i;
}
StringBuilder和StringBuffer:
StringBuilder(线程不安全,效率高),StringBuffer(线程安全,有同步代码块,效率低),其余2者一样。String是不可变字符序列。StringBuilder、StringBuffer和String里面用的都是char数组。
try {
throw new Exception(); //手动抛出一个异常
} catch (Exception e) {
e.printStackTrace();
}
ArrayList:
容器的底层实现很多是数组,
package cn.bjsxt.myCollection;
import java.util.ArrayList;
/**
* 模拟实现JDK中提供的ArrayList类
* @author dell
*
*/
public class MyArrayList {
/**
* The value is used for object storage.
*/
private Object[] value;
/**
* The size is the number of objects used.
*/
private int size;
public MyArrayList(){
this(10);//调用本类的构造函数
}
public MyArrayList(int size){
if(size<0){
try {
throw new Exception(); //手动抛出一个异常
} catch (Exception e) {
e.printStackTrace();
}
}
value = new Object[size];
}
public int size(){
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void add(Object obj){
value[size] = obj;
size++;
if(size>=value.length){
//装不下了。扩容吧!
int newCapacity = value.length*2;
Object[] newList = new Object[newCapacity];
// System.arraycopy(src, srcPos, dest, destPos, length);
for(int i=0;i<value.length;i++){
newList[i] = value[i];
}
value = newList;
}
}
public Object get(int index){
rangeCheck(index);
return value[index];
}
public int indexOf(Object obj){
if(obj==null){
return -1;
}else{
for(int i=0;i<value.length;i++){
if(obj==value[i]){
return i;
}
}
return -1;
}
}
public int lastIndexOf(Object obj){
if(obj==null){
return -1;
}else{
for(int i=value.length-1;i>=0;i--){
if(obj==value[i]){
return i;
}
}
return -1;
}
}
public Object set(int index, Object object) {
rangeCheck(index);
Object old = value[index];
value[index] = object;
return old;
}
public void rangeCheck(int index){
if(index<0||index>size-1){ //[0,size-1]
try {
throw new Exception(); //手动抛出一个异常。 讲到异常章节再说,先混个眼熟
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MyArrayList list = new MyArrayList(2);
list.add("aaa");
list.add(new Human("高琪"));
list.add("bbbb");
list.add("bbbb");
list.add("bbbb");
list.add("bbbb");
ArrayList list2;
Human h = (Human) list.get(1);
System.out.println(h.getName());
System.out.println(list.get(3));
System.out.println(list.size());
Human h1 = new Human("啊啊");
Human h2 = new Human("啊啊");
System.out.println("----------------------");
System.out.println(h1==h2);//false
System.out.println(h1.equals(h2));//false
}
}
(冒泡排序,二分法查找)
多维数组:
数组中对象排序是按自己定义的compareTo方法排序(对象类中自己写),