1. 程式人生 > >JAVA期末考試整理

JAVA期末考試整理

Technical problem:

0.read: Scanner input= new Scanner(System.in)

  1. random#: x=(int)(Math.random()*10) ,makes 0<=x<10.

Random class: Random ran=new Random(3);

Ran.nextInt();

  1. Switch(status){

Case 0: balabala;

Break;

Case 1:balabala;

Break;

Default: balabala;

//System.exit(1);

}

  1. Conditional expressions

Y=(x>0)?1:-1;

 

  1. op precedence

1==1^1&&1||1

 

  1. Math:ceil,floor, rint , round

6.char:

(int)char

isUpperCase

toLowerCase

When reading a char,using nextLine(),and use line.charAt(0);

  1. String

S1.charAt(1)

S1.concat(s2) <=>s3=s1+s2 (‘a’,2,+= is OK )

S1.equalsIgnoreCase(s2)

S1.compareToIgnoreCase(s2)

S1.endsWith(suffix2)

S1.contains(s2)

S1.substring(0,1) not include 1;

S1.indexOf(ch,fromIndex)

S1.lastIndexof(S,fromIndex)

X=Integer.parseInt(s1); converting to int

 

  1. formatting output

System.out.printf(“%4.2f”,x);

 

  1. Passing arguments of functions

Swap :hard to implement

Array is ok;

 

 

  1. Array:int[] A = new int[100];

Int[][] dis= new int[5][5];

A.length

For(int t:a){}

A1=a2;

Function with array Print(int[] array)    print(new int[]{1,2,3})

Variable length arg func printMax(double... number)

Arrays.sort(a)

Arrays.binarySearch(a,11)

Arrays.equals(a1,a2)

Arrays.fill(a1,5)

 

  1. Class

toString

Constructor

It’s Reference type

Static :class variable

Public: without it,the method can be only used in the package.

Private:”encapsulation” using set&&get function to modify

Protected:access by subclass in another package;

 

OBJECTIVE THINKING

Extends:

Super() :constructor

Super.method

 

  1. ArrayList

ArrayList<String> ss= new ArrayList<>(Arrays.asList(a));

Ss.size();ss.add(“A”);ss.indexOf(“A”);ss.get(0);ss.remove(“A”);ss.isEmpty();

Collections.sort(list);Collections.max(list);

  1. throw:
    public void method()

Throw Exception1,Exception2...

 

IllegalArgumentException ex= new IllegalArgumentException(“wrong arg”);

Throw ex;

 

Try

Catch

 

  1. File Class

File file=new FILE(“input.txt”)

File.length();

File.exists();

 

PrintWriter output=new PrintWriter(filename);

Output.print();

 

Scanner input=new Scanner(file);