Day11 顺序表(一)
1、对象:对象就是类的一个实例,有状态和行为。比如一只狗子是一个对象,狗子的状态有颜色、品种、名字,行为有吃饭、睡觉等。构造了一个具体的对象,我们就可以用其下的方法了;
2、类:就是一个模板,类用来描述一类对象的状态和行为。也可以说类是集合,对象就是集合中的元素, int i 中, int 就是集合, i 就是类型;
3、方法:方法就是行为,一个类可以有很多种方法;
4、包:包可以用来对类和接口进行分类;
5、修饰常量用 final ,final 只能用来修饰类或者方法,对访问控制类型用的有 private 、 protected 、 public 、 default ;
1 public class SequentialList { 2 3 /** 4 * The maximal length of the list. It is a constant. 5 */ 6 public static final int MAX_LENGTH = 10; 7 8 /** 9 * The actual length not exceeding MAX_LENGTH. 10 */ 11 int length; 12 13 /** 14 * The data stored in an array. 15 */ 16 int[] data; 17 18 /** 19 * Construct an empty sequential list. 20 */ 21 public SequentialList() { 22 length = 0; 23 data = new int[MAX_LENGTH]; 24 } 25 26 /** 27 * Construct a sequential list using an array. 28 * 29 * @param paraArray 30 */ 31 public SequentialList(int[] paraArray) { 32 data = new int[MAX_LENGTH]; 33 length = paraArray.length; 34 35 // Copy data. 36 for (int i = 0; i < paraArray.length; i++) { 37 data[i] = paraArray[i]; 38 } // Of for i 39 }// Of for the second constructor 40 41 /** 42 * Overrides the method claimed in Object, the superclas of any class. 43 */ 44 public String toString() { 45 String resultString = ""; 46 47 if (length == 0) { 48 return "empty"; 49 } 50 51 for (int i = 0; i < length - 1; i++) { 52 resultString += data[i] + ", "; 53 } // Of for i 54 55 //the last member of resultString 56 resultString += data[length - 1]; 57 58 return resultString; 59 }// Of for toString 60 61 public void reset() { 62 length = 0; 63 }// Of reset 64 65 /** 66 * The entrance of the program. 67 * 68 * @param args 69 */ 70 public static void main(String args[]) { 71 int[] tempArray = { 1, 4, 6, 9 }; 72 SequentialList tempFirstList = new SequentialList(tempArray); 73 System.out.println("Initialized, the list is: " + tempFirstList.toString()); 74 System.out.println("Again, the list is: " + tempFirstList); 75 76 tempFirstList.reset(); 77 System.out.println("After reset, the list is: " + tempFirstList); 78 }// Of main 79 }// Of class SequentialList

浙公网安备 33010602011771号