当前位置:   article > 正文

Java知识点总结_java知识体系

java知识体系

Java发展历程与语言特点:

跨平台、适合于分布式计算环境的纯面向对象语言,Java是一种编程语言, 又是一种平台和规范

语言特点:(JAVA是一种“简单、面向对象、分布式、解释型、健壮、安全、体系结构中立、可移植、高性能和动态”的编程语言)

1)简单(完全面向对象的,它是最容易学习的面向对象的编程语言之一,同时它还提供了大量可重用的类库,Java的简单性是以增加系统的复杂性为代价的)

2)面向对象(支持封装、继承、多态)

3)分布式数据分布支持(通过Java的URL类可以访问网上的各类信息资源,访问方式完全类似于本地文件系统)、操作分布支持(通过在web页面中的小应用程序(Applet)等将计算从服务器分布至客户机,避免网络拥挤,提高系统效率))

4)半编译、半解释(Java源程序经编译器编译后产生字节码(bytecode),Java解释器解释执行字节码,优点:     兼具编译执行的效率优势和解释执行的灵活性;  提高了应用程序的可移植性:源程序、中间代码)

5)平台无关性/可移植性(程序不经过修改而在不同的硬件或软件平台上运行的特性,Java可移植性源于两方面:   Java的半编译、半解释的特征;   Java采用标准的独立于硬件平台的数据类型)

6)安全性(Java的内存分配和引用模型对于程序员透明,完全由底层的运行系统控制。在语言和运行环境中引入了多级安全措施:内存布局与C、 C++不同,不是由编译器决定,而是运行系统根据所在的硬件平台的特性决定; 取消了C、C++的指针,JAVA编译器通过符号指针来引用内存,由JAVA运行系统在运行时将符号指针具体解释为实际的内存地址。字节码验证:运行系统引入字节码验证器,其中包含简单的规则验证程序,以确保遵循规则)

7)多线程

8)健壮(Java致力于检查程序在编译和运行时的错误。 运行错误的处理机制 内存的垃圾收集机制 不使用指针操作)

不同操作系统环境的设置与编译运行命令:

开发与运行Java程序需要经过的三个主要步骤为:编辑源程序编译生成字节码解释运行字节码

• It translates a Java program into byte-code.(Byte-code is the machine language for a hypothetical computer (or interpreter) called the Java Virtual Machine, a byte-code program is easy to translate into machine language for any particular computer.)

• A program called an interpreter translates each byte-code instruction, executing the resulting machine-language instructions on the particular computer before translating the next byte-code instruction.

• Most Java programs today are executed using a Just-In-Time or JIT compiler in which byte-code is compiled as needed and stored for later reuse without needing to be re-compiled.

• Use the compiler to translate the Java program into bytecode (done using the javac command)

· the byte-code can be used on any computer with a byte-code interpreter and without a need to recompile, so it can be sent over the Internet and use anywhere in the world. So this makes Java suitable for Internet applications.

运行环境的种类: JAVA Application (JAVA解释器)、 JAVA Applet(JAVA兼容的Web浏览器)

JDK工具:

appletviewer: Applets是被嵌入在网页里的Java程序,可以用Web浏览器运行appletsappletviewer 可运行applets而不必运行 Web浏览器。 用法: appletviewer < .HTML文件的URL>

javap反汇编程序: javap反汇编程序可以从字节码文件恢复 java 源代码。 用法:        javap <.class文件列表> 文件名由空格分隔

典型Java程序:字符与图形界面简介

• JavaFX is a set of packages that allow Java programmers to create graphics and media applications

JavaFX是最新的图形化工具包,程序员可以创建富图形化和富媒体应用程序

基本元素:关键字 表达式 标识符 运算符 分支和循环控制

在Java的基本数据类型中,char型采用Unicode编码方案,每个Unicode码占用 2 字节内存空间。

不是关键字:sizeof,字面常量:true、false、null

标识符

 

 

 若想对比两个对象的实际内容是否相同,使用方法equals(),但不适用于自己创建的类!

 

若为String,则使用s1.compareTo(s2) 

 

 

 

八种基本数据类型的定义与使用

Float类型的数据其最后一个字符之后需加入“F”或“f”,如果数据其最后一个字符之后没有“F”或“f”,则默认为double类型的数值。

reference 类型:数组Array ,类 classe,接口interface。reference 类型的变量可简单认为是所引用对象的内存地址。

String类

 String不是基本数据!

创建字符串变量:

  1. S=new String(“abc”);
  2. S=“abc”;
  3. String tomName=String(“tom”);
  4. String(char a[]);//通过数组创建字符串对象
  5. String(char a[],int startIndex,int count);//截取数组元素的一部分来创建字符串对象
  1. s1.length();//获取字符串的长度
  2. charAt(int index);//获得字符串指定位置的字符
  3. //比较
  4. String s1=“abc”,s2=“abc”,s3=“Abc”;
  5. s1.equals(s2); //true
  6. s1.equals(s3); //false
  7. s1==s2; //false,引用
  8. //equalsIgnoreCase(String s),比较时忽略大小写
  9. String s1=“abc”,s2=“Abc” ;
  10. s1. equalsIgnoreCase(s2); //true
  11. //public boolean startsWith(String s):判断当前字符串的前缀是否含有字符串s
  12. String s1=“220123456”,s2=”210654321”;
  13. s1. startsWith(“220”); //true
  14. s2. startsWith(“220”); //false
  15. //public boolean endsWith(String s):判断当前字符串的后缀是否含有字符串s
  16. s1. endsWith(“456”); //true
  17. //public int compareTo(String s):按字典顺序比较当前字符串和s
  18. //public int compareToIgnoreCase(String s):比较时忽略大小写
  19. s1.compareTo(s2);
  20. //public int indexOf(String s)返回第一次找到时的下标,如果没有找到,则返回-1
  21. //public int indexOf(String s, int strartPoint)从指定的下标开始找子串,返回第一次找到时的下标,找不到则返回-1
  22. String s=“I am a good student”;
  23. S.indexOf(“a”); //=2
  24. S.indexOf(“a”,3); //=5
  25. S.indexOf(“k”,2); //=-1
  26. //字符串的截取(求子串)
  27. "unhappy".substring(2);// returns "happy"
  28. "Harbison".substring(3);// returns "bison“
  29. "emptiness".substring(9);// returns "" (an empty string)
  30. "hamburger".substring(4, 8);// returns "urge"
  31. "smiles".substring(1, 5);// returns "mile"
  32. //拼接两个字符串,并返回一个新字符串,源字符串不会被修改
  33. String s1 = "ABC";
  34. String s2 = "XYZ";
  35. s1 = s1.concat(s2); // s1 = s1 + s2;
  36. //替换public String replace(char oldChar, char newChar)
  37. String s1 = "mesquite in your cellar";
  38. s1.replace('e', 'o');// returns "mosquito in your collar"
  39. //public String replaceAll(String regex, String replacement)
  40. String s1 = "opq@#$";
  41. s1.replaceAll(“[a-z]”,”123”);// returns “123123123@#$”
  42. //返回前后没有空格的字符串public String trim()
  43. "Java ".trim();//"Java"
  44. //字符串与相应数值的相互转化public static int parseInt(String s)
  45. int i=Integer.parseInt(“123”);
  46. public static float Float.parseFloat(String s)
  47. public static double Double.parseDouble(String s)
  48. //通过String类的静态(static)成员方法valueOf(…)转为String类型
  49. public String valueOf(byte n)
  50. public String valueOf(int n)
  51. public String valueOf(long n)
  52. public String valueOf(float n)
  53. public String valueOf(double n)
  54. String str=String.valueOf(12313.9876);
  55. Float x=123.987f;
  56. String s=String.valueOf(x);
  57. //对象还可以通过方法toString转化成字符串
  58. Date date=new Date();
  59. System.out.println(date.toString());//Sat Oct 04 21:10:48 CST 2007

求String类对象长度:s.length()//这里有()!!

在android系统下,默认是UTF8编码,一个中文字符通常相当于3个字节,而gb2312下一个中文相当于2字节。把JVM内存中unicode形式的String按encoding指定的编码,转成字节流。这种情况下可采取以下办法:

  1. byte[] byBuffer = new byte[200];
  2. String strInput="我是字符串";
  3. byBuffer= strInput.getBytes("gb2312");

JVM内部的String,Char都是用unicode存储(没有任何特殊编码) 

  1. buf1 = new StringBuffer();//创建空的StringBuffer对象容量为16字符
  2. buf2 = new StringBuffer( capacity );//创建空的StringBuffer对象指定容量大小
  3. buf3 = new StringBuffer( myString );//创建含有相应字符序列的StringBuffer对象容量为myString.length() + 16
  4. //实例
  5. StringBuffer b = new StringBuffer("hello");

屏幕输出与键盘输出

  1. import java.util.Scanner;
  2. //Scanner Scanner_Object_Name = new Scanner(System.in);
  3. Scanner Keyboard = new Scanner(System.in);
  4. int n1 = keyboard.nextInt();//读入int
  5. String s1 = keyboard.next();//读入String
  6. String sentence = keyboard.nextLine();//读入一行

类和方法的定义,成员的封装与隐藏,对象与引用

基本概念:事物的属性或特性可以用变量来表示,行为或功能用方法来反应。

是同种对象的集合与抽象:在面向对象的程序设计中,定义类的概念来表述同种对象的公共属性和特点。 类是一种抽象的数据类型,它是具有一定共性的对象的抽象,而属于类的某一对象则被称为是类的一个实例,是类的一次实例化的结果。

面向对象的程序设计方法就是将客观事物抽象成为“类”,并通过类的“继承”实现软件的可扩充性和可重用性。

 

 

 

 

 

构造器与方法重载

 

声明对象:声明对象的名字和类型,用类名来说明对象的类型。 格式:type name

说明: 声明对象的规则与变量声明规则相同,但对象变量是引用类型; 在java里类和接口都可以作为数据类型来使用; 对象声明通知编译器name将用来引用type类型的变量 对象声明并不创建新的对象。操作符new通过为新对象分配存储空间来实例化类

 

 System.gc( ); // 申请立即回收垃圾

static关键字/类成员与实例成员:在类中声明一个变量或方法时,可指定它为实力成员或类成员。用static修饰的是类成员(类变量oe类方法)

类变量与实例变量的区别:1)运行时系统在首次遇到类变量时就为类变量分配存储空间;而当创建类的实例对象时才为实例变量分配存储空间。2)不管有多少类的实例,类变量只有一份拷贝,即所有实例共享相同的类变量;而每个实例对象都有自己的实例变量。3)实例变量只能通过对象来访问,而类变量既可以通过类名来访问,也可以通过实例对象来访问。

在该类所有实例之间是共享的。在加载该类时,只分配 一次空间,并初始化。可用来在实例之间进行通信或跟踪该类实例的数目。(e.g.用count++来计数)

 类方法与实例方法的区别:1)实例方法既能访问当前对象的实例变量也能访问变量;类方法只能访问类变量而能访问实例变量。2)实例方法只能由实例对象来调用而类方法既可以由实例对象调用也可以由类名直接调用。

 

 Java总是会在尝试使用自动类型转换之前使用重载。如果Java可以找到一个能够匹配引元类型的方法定义,就会使用这个定义。Java尝试寻找参数类型与方法调用中的引元精确匹配的方法名定义,直至尝试失败之后才会尝试方法引元的自动类型转换。(先找是否有重载函数再取进行自动类型转换)

 

 

 

 

 

 

 

 default(缺省访问,包范围的): 如果没有给定修饰符,那就是default或friendly(友元)的(它不是java的关键字)。拥有缺省修饰符的类、变量或方法对同一个包里的所有类来说都是可访问的。

 一维与多维数组的定义与实现

获取数组长度:c.length(注意没有()),c.length是final变量,不能对它 再次赋值

每个数组元素是对象的引用,还需要继续创建对象才可以使用。

数组的初始化语句创建数组 int[ ] n = { 10, 20, 30, 40, 50 };

·对基本数据类型:

  1. int[] nums;
  2. nums = new int[3];
  3. nums[0] = 1;
  4. nums[1] = 2;
  5. nums[2] = 3;

·对非基本数据类型:

  1. MyDate[] dates;
  2. dates = new MyDate[ 3];
  3. dates[ 0] = new MyDate( 22, 7, 1964);
  4. dates[ 1] = new MyDate( 1, 1, 2000);
  5. dates[ 2] = new MyDate( 22, 12, 1964);
  6. MyDate[] dates = {
  7. new MyDate( 22, 7, 1964),
  8. new MyDate( 1, 1, 2000),
  9. new MyDate( 22, 12, 1964) };

当指定数组参数时,只需要给出数组的基类型,但不需要指定数组的长度。

  1. double[] a = new double[10]
  2. double[] b = new double[30]
  3. SampleClass.incrementArrayBy2(a);
  4. SampleClass.incrementArrayBy2(b);

多维数组定义方式:

  1. int [][] intArray;
  2. int[][][] a2;

分配内存空间:

  1. int[][] a = new int[2][3];//直接为每一维分配空间
  2. int twoDim [][] = new int [][ 4]; //error
  3. //分别为每一维分配空间
  4. int[][] a = new int[2][ ];
  5. a[0] = new int[3];
  6. a[1] = new int[3];
  7. //可以为每行设置为空间大小不同的数组
  8. a[0] = new int[3];
  9. a[1] = new int[5];

初始化:

  1. int a[][] = {{2,3}, {1,5}, {3,4}};
  2. double[ ][ ] c =
  3. {
  4. {1.0, 2.0, 3.0, 4.0},
  5. {0.0, 1.0, 0.0, 0.0},
  6. {0.0, 0.0, 1.0, 0.0}
  7. };

数组数据的复制,通过拷贝数组的函数

  1. System.arrayCopy(Object source, int srcIndex, Object dest, int destIndex, int length)
  2. public class ArrayCopyDemo {
  3. public static void main(String[] args) {
  4. char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n',
  5. 'a', 't', 'e', 'd' };
  6. char[] copyTo = new char[7];
  7. System.arraycopy(copyFrom, 2, copyTo, 0, 7);
  8. System.out.println(new String(copyTo));
  9. }
  10. }

继承与多态的Java描述与实现

类java.lang.Object处于类层次树的根部,其他所有的类都直接或间接地为它的子类

 

 

 

 

 

 

 抽象类中不一定要包含abstract方法,但是,一旦某个类中包含了abstract方法,则这个类必须声明为abstract类

使用多态进行程序设计的核心技术之一是使用上转型对象,即将abstract类声明对象作为其子类的上转型对象,那么这个上转型对象就可以调用子类重写的方法。 所谓面向抽象编程,是指当设计某种重要的类时,不让该类面向具体的类,而是面向抽象类,即所设计类中的重要数据是抽象类声明的对象,而不是具体类声明的对象。

  1. //Geometry.java
  2. public abstract class Geometry { //如果使用接口需用interface来定义Geometry。
  3. public abstract double getArea();
  4. }
  5. Circle.java
  6. public class Circle extends Geometry {
  7. double r;
  8. Circle(double r) {
  9. this.r=r;
  10. }
  11. public double getArea() {
  12. return(3.14*r*r);
  13. }
  14. }
  15. //Rectangle.java
  16. public class Rectangle extends Geometry {
  17. double a,b;
  18. Rectangle(double a,double b) {
  19. this.a=a; this.b=b;
  20. }
  21. public double getArea() {
  22. return a*b;
  23. }
  24. }
  25. //Pillar.java
  26. public class Pillar {
  27. Geometry bottom; //bottom是抽象类Geometry声明的变量
  28. double height;
  29. Pillar (Geometry bottom,double height) {
  30. this.bottom=bottom; this.height=height;
  31. }
  32. public double getVolume() {
  33. return bottom.getArea()*height; //bottom可以调用子类重写的getArea方法
  34. }
  35. }
  36. //Application.java
  37. public class Application{
  38. public static void main(String args[]){
  39. Pillar pillar;
  40. Geometry bottom;
  41. bottom=new Rectangle(12,22);
  42. pillar =new Pillar (bottom,58); //pillar是具有矩形底的柱体
  43. System.out.println("矩形底的柱体的体积"+pillar.getVolume());
  44. bottom=new Circle(10);
  45. pillar =new Pillar (bottom,58); //pillar是具有圆形底的柱体
  46. System.out.println("圆形底的柱体的体积"+pillar.getVolume());
  47. }
  48. }

接口定义、继承和实现

 

 

 

 

 

 

 

 

 

 

 

 

必须实现接口中定义的所有方法。 如果一个类声明实现一个接口,但没有实现接口中的所有方法,那么这个类必须是abstract类.

接口可以作为一种引用类型来使用,所以可以象其它类型的一样使用。把接口作为一种数据类型可以不需要了解对象所对应的具体的类,而着重于它的交互接口 使用者关注的是对象所实现(或暴露)的接口(能力)而不是目标对象本身。

  1. interface ShowInfo{
  2. void showBrand(String s);
  3. }
  4. class TV implements ShowInfo{
  5. public void showBrand(String s){
  6. System.out.printf(s);
  7. }
  8. }
  9. class PC implements ShowInfo{
  10. public void showBrand(String s){
  11. System.out.printf(s);
  12. }
  13. }
  14. public class Exam{
  15. public static void main(String args[]){
  16. ShowInfo si;
  17. si = new TV();
  18. si.showBrand(“索尼牌电视机”); //接口回调
  19. si = new PC();
  20. si. showBrand(“DELL PC”); //接口回调
  21. }
  22. }

 在接口类中,对象只能调用接口中的方法,不能使用继承类的方法,若要使用,则需要进行强制类型转换

 

异常机制

什么是异常(Exception)(在其他书中,也称例外)? 异常(Exception)是正常程序流程所不能处理或没有处理的异常情况或异常事件。 

 格式:

在try语句块中包含可能会产生异常的语句

紧接着若干个catch语句块,进行异常处理

catch语句块与finally语句块至少存在一个

  1. try
  2. {
  3. // code that may throw exceptions
  4. }
  5. catch (ExceptionType ref)
  6. {
  7. //exception handling code
  8. }
  9. finally
  10. {
  11. // …
  12. }

 常见异常:内存耗尽、数组下标越界、除数为0、非法的参数(方法的参数)

 

 

  1. public class ExceptionMethods {
  2. public static void main(String[] args) {
  3. try {
  4. throw new Exception("Here's my Exception");
  5. } catch(Exception e) {
  6. System.out.println("Caught Exception");
  7. System.out.println("e.getMessage(): " + e.getMessage());
  8. System.out.println("e.toString(): " + e.toString());
  9. System.out.println("e.printStackTrace():");
  10. e.printStackTrace();
  11. }
  12. }
  13. }

 用户也可自定义异常类来处理异常; 自定义异常类是Exception类的子类,可包含普通类的内容。

当程序运行中发生异常时,异常处理机制将按以下步骤处理: 1、产生异常对象并中断当前正在执行的代码,抛出异常对象。 2、自动按程序中的catch的编写顺序查找“最接近的”异常匹配。一旦找到就认为异常已经得到控制,不再进行进一步查找。这个匹配不需要非常精确,子类对象可以与父类的catch相匹配。 3、若有匹配则执行相应的处理代码,然后继续执行本try块之外的其他程序。否则这个没有被程序捕获的异常将由缺省处理程序处理,缺省处理程序将显示异常的字符串、异常发生位置等信息,终止整个程序的执行并退出。

catch进入过一次后面的都会忽略掉

try、catch中有return时,也会执行finally。return时,要注意返回值的类型,是否受到finally中代码的影响。

finally中有return时,会直接在finally中退出,导致try、catch中的return失效。在执行完finally的return之后,就不会再返回try中的return值。

  1. public class Demo8 {
  2. //引用数据类型
  3. //最终返回结果是finally的结果,方法在处理返回值的时候finally块执行
  4. public static void main(String[] args) {
  5. Person p = ha();
  6. System.out.println(p.age);
  7. }
  8. public static Person ha() {
  9. Person p = new Person();
  10. try {
  11. p.age = 18;
  12. return p;
  13. } catch (Exception e) {
  14. return null;
  15. } finally {
  16. p.age = 28;
  17. }
  18. }
  19. static class Person{
  20. int age;
  21. }
  22. }//28
  1. public int catch1() {
  2. int i = 1;
  3. try{
  4. i++;
  5. int x = i / 0 ;
  6. }catch (Exception e) {
  7. i++;
  8. return i;
  9. }finally {
  10. i++;
  11. }
  12. return i;
  13. }//catch中会先执行return前的代码,然后暂时保存需要return的信息,再执行finally中的代码,最后再通过return返回之前保存的信息。所以,这里方法返回的值是try、catch中累积计算后的3,而非finally中计算后的4。
  1. public int catch1() {
  2. int i = 1;
  3. try{
  4. i++;
  5. return i;
  6. }catch (Exception e) {
  7. i++;
  8. }finally {
  9. i++;
  10. return i;
  11. }
  12. }//3
  1. static void createException() {
  2. throw new ArrayIndexOutOfBoundsException();
  3. }
  4. static void method() {
  5. try {
  6. createException();
  7. System.out.print('a');
  8. } catch (ArithmeticException e) {
  9. System.out.print('b');
  10. } finally {
  11. System.out.print('c');
  12. }
  13. System.out.print('d');
  14. }
  15. }//cmn
  1. class TestException {
  2. public static void main(String[] args) {
  3. try {
  4. method();
  5. } catch (Exception e) {
  6. System.out.print('m');
  7. }
  8. System.out.println('n');
  9. }
  10. static void createException() {
  11. throw new ArrayIndexOutOfBoundsException();
  12. }
  13. static void method() {
  14. try {
  15. createException();
  16. System.out.print('a');
  17. } catch (ArrayIndexOutOfBoundsException e) {
  18. System.out.print('b');
  19. } finally {
  20. System.out.print('c');
  21. }
  22. System.out.print('d');
  23. }
  24. }//bcdn
  1. class TestException {
  2. public static void main(String[] args) {
  3. try {
  4. method();
  5. } catch (Exception e) {
  6. System.out.print('m');
  7. }
  8. System.out.println('n');
  9. }
  10. static void createException() {
  11. throw new ArrayIndexOutOfBoundsException();
  12. }
  13. static void method() {
  14. try {
  15. createException();
  16. System.out.print('a');
  17. } catch (ArithmeticException e) {
  18. System.out.print('b');
  19. } finally {
  20. System.out.print('c');
  21. }
  22. System.out.print('d');
  23. }
  24. }//cmn


 

二进制流与文件流类

FileOutputStream 在写文件内容时,若目标文件已存在,没有警告信息
File的实例对象可以判断出文件是否存在 提供警告信息,或改变打开文件方式

流式文件读写

  1. //文件流的建立
  2. FileInputStream in=new FileInputStream(fp);
  3. FileOutputStream out=new FileOutputStream(fp);
  1. //文件拷贝 file1->file2
  2. import java.io.*;
  3. public class filestream{
  4. public static void main(String args[]){
  5. try{
  6. File inFile=new File("file1.txt");
  7. File outFile=new File("file2.txt");
  8. FileInputStream fis=new FileInputStream(inFile);
  9. FileOutputStream fos=new FileOutputStream(outFile);
  10. int c;
  11. while((c=fis.read())!=-1) fos.write(c); //核心代码
  12. fis.close(); fos.close();
  13. }
  14. catch(FileNotFoundException e) {
  15. System.out.println("FileStreamsTest: "+e);
  16. }
  17. catch(IOException e) {
  18. System.err.println("FileStreamsTest: "+e);
  19. }
  20. }
  21. }
  1. //拷贝文件
  2. import java.io.*;
  3. public class Copy {
  4. public static void main(String[] args) throws IOException {
  5. File inputFile = new File("farrago.txt");
  6. File outputFile = new File("outagain.txt");
  7. FileReader in = new FileReader(inputFile);
  8. FileWriter out = new FileWriter(outputFile);
  9. int c;
  10. while ((c = in.read()) != -1)
  11. out.write(c);
  12. in.close();
  13. out.close();
  14. }
  15. }
  1. //创建File类
  2. myFile = new File("myfile.txt");
  3. File myDir = new File("MyDocs");
  4. myFile = new File( myDir, "myfile.txt");
  5. File f2 = new File("/etc", "passwd");
  1. //如果要读入类的话要implemtnts Serializable
  2. public class Species implements Serializable{}
  3. public class ClassObjectIODemo
  4. {
  5. public static void main(String[] args)
  6. {
  7. ObjectOutputStream outputStream = null;
  8. ObjectInputStream inputStream = null;
  9. String filename = "species.records";
  10. try
  11. {
  12. outputStream = new ObjectOutputStream(new FileOutStream(filename));
  13. outputStream.writeObject(Species s);
  14. readOne = (Species)inputStream.readObject();//注意强制转换
  15. }catch(IOException e){
  16. System.out.println("Errpr````");
  17. }
  18. }
  19. }

​​​​​​​

泛型与动态数据结构(集合框架、链式数据结构)(只考ArrayList)

可提高程序代码的复用性

减少数据的类型转换,以提高代码的运行效率     --在编译时强制使用正确的数据类型

通过给类或接口增加类型参数实现

ArrayList:采用可变大小的数组实现List接口,并提供了访问数组大小的方法。ArrayList对象会随着元素的增加其容器自动扩大。该类是非线程同步的,3种List效率最高也最常用。

 

 

 

  1. import java.util.Scanner;
  2. public class ArrayListDemo
  3. {
  4. public static void main(String[] args){
  5. ArrayList<String> toDoList = new ArrayList<Atring>();
  6. System.out.println("Enter items for the list, when prompted.");
  7. bool done = false;
  8. Scanner keyboard = new Scanner(Syatem.in);
  9. while(!done)
  10. {
  11. System.out.print("Type an entry:");
  12. String entry = keyboard.nextLine();
  13. toDoList.add(entry);
  14. System.out.print("More item for the list?");
  15. String ans = keyboard.nextLine();
  16. if(!ans.equalsIgnoreCase("yes")) done = true;
  17. }
  18. System.out.println("The list contains:");
  19. int listSize = toDoList.size();
  20. for(int position = 0; position<listSize; position++) System.out.println(toDoList.get(position));
  21. }
  22. }

多线程(只考概念)

 

接口中的方法

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/802824
推荐阅读
相关标签
  

闽ICP备14008679号