JAVA语法题

时间:2022-06-16 13:33:54

import java.util.*;

public class Birthdays {
public static void main(String[] args){
Map<Friends, String> hm = new HashMap<Friends,String>();
Friends f1 = new Friends("Charis");
Friends f2 = new Friends("Darling");
hm.put(f1, "summer 2009");
hm.put(f2, "spring 2002");
System.out.println(hm.get(f1));
Friends f3 = new Friends("Darling");
System.out.println(hm.get(f3));
}
}
class Friends{
String name;
Friends(String n){
name = n;
}
}

结果:

summer 2009
null

  

2.  enum用法 

public class WeatherTest {
static Weather w;
public static void main(String[] args){
System.out.println(w.RAINY.count+" "+w.Sunny.count+" ");
}
}
enum Weather{
RAINY, Sunny;
int count = 0;
Weather(){
System.out.print("c ");
count++;
}
}

结果

c c 1 1
public class WeatherTest {
static Weather w;
public static void main(String[] args){
System.out.println(w.RAINY+" "+w.Sunny+" ");
}
}
enum Weather{
RAINY(1), Sunny(2);
private int n;
private Weather(int n){
this.n=n;
}
@Override
public String toString() {
return String.valueOf(this.n);
}
}

结果

1 2

枚举的遍历:

public class WeatherTest {
enum Weather{
RAINY(1), Sunny(2);
private int n;
private Weather(int n){
this.n=n;
}
@Override
public String toString() {
return String.valueOf(this.n);
}
}
public static void main(String[] args){
Weather[] allW = Weather.values();
for(Weather w: allW)
System.out.print(w+" ");
}
}

结果同上, 1 2

3. 数值类型题

public class Dec26 {

    public static void main(String[] args){
short a1=6;
new Dec26().go(a1);
new Dec26().go(new Integer(7));
} void go(short x){
System.out.print("S ");
}
void go(Long x){
System.out.print("L ");
}
void go(int x){
System.out.print("i ");
}
void go(Number n){
System.out.print("N ");
} }

结果:

S N

4. switch, case  

public class Humping {
public static void main(String[] args) {
String r="-";
char[] c={'a','b','c','z'};
for(char c1:c)
switch(c1){
case 'a':
r+="a";
case 'b':
r+="b";
break;
default:
r+="X";
case 'z':
r+="z";
}
System.out.println(r);
}
}

结果:

-abbXzz

5. HashMap键值为空的问题

HashMap:适用于在Map中插入、删除和定位元素。 
Treemap:适用于按自然顺序或自定义顺序遍历键(key)。

两者都是非线程安全,前者无排序,后者会自动排序

Ø  HashMap里面存入的键值对在取出的时候是随机的,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。在Map 中插入、删除和定位元素,HashMap是最好的选择。

Ø  TreeMap取出来的是排序后的键值对。插入、删除需要维护平衡会牺牲一些效率。但如果要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。

本测试增加和查找功能,HashMap比TreeMap的效率要高。

import java.util.*;
public class Garage { public static void main(String[] args) {
Map<String,String> hm=new HashMap<String,String>();
String[] k={null,"2","3",null,"5"};
String[] v={"a","b","c","d","e"};
for(int i=0;i<5;i++){
hm.put(k[i],v[i]);
System.out.print(hm.get(k[i])+" ");
}
System.out.print(hm.size()+" "+hm.values()+"\n");
}
}

结果

a b c d e 4 [c, d, b, e]

换成TreeMap报错, 所以 TreeMap键值不允许空 

null和" "的区别:

import java.util.*;
public class Garage { public static void main(String[] args) {
Map<String,String> hm=new HashMap<String,String>();
String[] k={null,"2","3"," ","5"};
String[] v={"a","b","c","d","e"};
for(int i=0;i<5;i++){
hm.put(k[i],v[i]);
System.out.print(hm.get(k[i])+" ");
}
System.out.print(hm.size()+" "+hm.keySet()+" "+hm.values()+"\n");
}
}

结果

a b c d e 5 [3, null, 2,  , 5] [c, a, b, d, e]

6. Exception

public class ClassA {
static String s="";
public static void main(String[] args){
try{
doStuff();
}
catch(Exception ex){
s+="c1 ";
}
System.out.println(s);
}
static void doStuff() throws RuntimeException{
try{
s+="t1 ";
throw new IllegalArgumentException();
}
catch(IllegalArgumentException ie){
s+="c2 ";
}
throw new IllegalArgumentException();
}
}

结果

t1 c2 c1

7. Arrays.binarySearch用法

如果要搜索的元素key在指定的范围内,则返回搜索键的索引;否则返回-1或者"-"(插入点)。
eg:
1.该搜索键在范围内,但不在数组中,由1开始计数;
2.该搜索键在范围内,且在数组中,由0开始计数;
3.该搜索键不在范围内,且小于范围内元素,由1开始计数;
4.该搜索键不在范围内,且大于范围内元素,返回-(endIndex + 1);(特列)

public class Unturned {
public static void main(String[] args){
String[] towns = {"aspen","vail","t-ride","dillon"};
//MySort ms=new MySort();
Arrays.sort(towns);
System.out.println(Arrays.binarySearch(towns,"dillon"));
}
}

结果: 1

import java.util.Arrays;
import java.util.Comparator; public class Unturned {
public static void main(String[] args){
String[] towns = {"aspen","vail","t-ride","dillon"};
MySort ms=new MySort();
Arrays.sort(towns,ms);
System.out.println(Arrays.binarySearch(towns,"dillon"));
}
static class MySort implements Comparator<String>{
public int compare(String a, String b) {
return b.compareTo(a);
}
}
}

结果: -1