一. 理解:
- 简单来说顺序表就是数组
- 数据结构与Java的对象有异曲同工之处
二. 其他你要知道的:
- 对象:数据及操作的总和。变量+方法。对象是类的实例。
- 类:几种理解方法:是对象的抽象。是对象的模板。类是集合,对象是元素。
- 包:不是必须的,但你好歹分个什么什么类呀。比如,你创建的不同的文件夹。
三. 你更要知道的:
- final:用于定义常量
//变量名大写不说了吧 //用处就是,你定义在哪个类,那么那个类任何地方你都可以用 final int MAX_LENGTH = 10;
- new:创建对象
class Student {
//变量
int id;
String name;
//创建对象必须要有,无参或有参,的构造方法
Student() {}
//方法
void write_homework() {
System.out.println("写作业");
}
}
public class Main {
public static void main(String[] args) {
Student cbb = new Student();
}
}
- 构造方法
这其实相当与JAVA的重载,后面讲
class Student {
//创建对象必须要有,无参或有参,的构造方法
Student() {} //无参构造方法
Student(int a) {} //有参构造方法,里面参数个数由你决定也可以Student(int a, String b)
}
- 构造方法的作用:赋值(最直接的作用)
class Student {
int id;
String name;
Student() {}
Student(int x) {
id = x;
}
Student(int x, String y) {
id = x;
name = y;
}
}
在主函数里面你就要:
Student s = new Student(); //就创建了一个名为s的对象
Student s1 = new Student(21);
Student s2 = new Student(21,"小明");
四. 数组
- 数组:同一类型,有序
- 下面介绍对数组操作的一些方法:
- toString()
这其实是对toString()方法的重写,后面讲
public String toString() {
return //你想怎么输出,输出什么样的格式,就那么写。
}
- 自定义方法:清空数组
reset()
public void reset() {
length = 0;
}
- 综合例子:
class Array {
final int MAX_LENGTH = 10;
int length;
int []arr;
Array() {
length = 0;
//对arr[]赋值
arr = new int[MAX_LENGTH];
}
Array(int[] a) {
length = a.length;
//对arr[]赋值
arr = new int[MAX_LENGTH];
for(int i=0; i


