栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA第八天——顺序表(二)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA第八天——顺序表(二)

顺序表

一. 理解:

  1. 简单来说顺序表就是数组
  2. 数据结构与Java的对象有异曲同工之处

二. 其他你要知道的:

  1. 对象:数据及操作的总和。变量+方法。对象是类的实例。
  2. 类:几种理解方法:是对象的抽象。是对象的模板。类是集合,对象是元素。
  3. 包:不是必须的,但你好歹分个什么什么类呀。比如,你创建的不同的文件夹。

三. 你更要知道的:

  1. final:用于定义常量
//变量名大写不说了吧
//用处就是,你定义在哪个类,那么那个类任何地方你都可以用
final int MAX_LENGTH = 10;
  1. 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();
	}
}
  1. 构造方法

这其实相当与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,"小明");

四. 数组

  • 数组:同一类型,有序
  • 下面介绍对数组操作的一些方法:
  1. toString()

这其实是对toString()方法的重写,后面讲

public String toString() {
	return //你想怎么输出,输出什么样的格式,就那么写。
}
  1. 自定义方法:清空数组
    reset()
	public void reset() {
		length = 0;
	}
  1. 综合例子:
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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/666518.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号