Java综合能力提升Day-6🐱‍🏍一、选择题🐱‍🏍二

这是我参与11月更文挑战的第10天,活动详情查看:2021最后一次更文挑战」 @TOC

🐱‍🏍 一、选择题

1:Java中的集合类包括ArrayList、LinkedList、HashMap等,下列关于集合类描述错误的是?

A ArrayList和LinkedList均实现了List接口
B ArrayList的访问速度比LinkedList快
C 随机添加和删除元素时,ArrayList的表现更佳
D HashMap实现Map接口,它允许任何类型的键和值对象

💯解析:ArrayList底层是数组,所以删除元素和随机添加的表现不如LinkedList。

🎖️正确答案:B


2:阅读下列程序,选择哪一个是正确的输出结果

class HelloA{
public HelloA()
{
System.out.println("I’m A class ");
}
static
{
System.out.println("static A");
}
}
public class HelloB extends HelloA{
public HelloB()
{
System.out.println("I’m B class");
}
static{
System.out.println("static B");
}
public static void main (String[] args){
new HelloB();
}
}
复制代码

A static A I’m A class static B I’m B class
B I’m A class I’m B class static A static B
C static A static B I’m A class I’m B class
D I’m A class static A I’m B class static B

💯解析:调用顺序;父类优先于子类,静态优先于非静态。所以应该是先打印父类的静态代码块,接着是子类的静态代码块,然后是父类构造方法、子类构造方法。

🎖️正确答案:C


3:执行下列代码的输出结果是( )

public class Demo{
public static void main(String args[]){
int num = 10;
System.out.println(test(num));
}
public static int test(int b){
try
{
b += 10;
return b;
}
catch(RuntimeException e)
{
}
catch(Exception e2)
{
}
finally
{
b += 10;
return b;
}
}
}
复制代码

A 10
B 20
C 30
D 40

💯解析:finally不管是否异常都会执行,所以应该是b+=10,两次。最终返回的是30

🎖️正确答案:C


4:下列代码的输出结果是_____

boolean b=true?false:true==true?false:true;
System.out.println(b);
复制代码

A true
B false
C null
D 空字符串

💯解析:
== 优先级高于三目运算符
故第一步执行:true==true,结果为true
此时表达式为boolean b = true?false:true?false:true
第二步三木运算符按照从右至左原则,true?false:false,结果为false
此时表达式为boolean b = true?false:false;
第三步结果为false

🎖️正确答案:B


🐱‍🏍二、编程题

2.1 把字符串转换成整数

链接:https://www.nowcoder.com/questionTerminal/1277c681251b4372bdef344468e4f2e
来源:牛客网
复制代码

将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为 0 或者字符串不是一个合法的数值则返回 0

数据范围:字符串长度满足
进阶:空间复杂度 ,时间复杂度

注意:
①字符串中可能出现任意符号,出现除 +/- 以外符号时直接输出 0
②字符串中可能出现 +/- 且仅可能出现在字符串首位。

示例1
输入
"+2147483647"
输出
2147483647
复制代码

题目链接在这里

解析:分成三种情况讨论。
1:开头第一个字符为‘+’或者是‘-’的
2:字符串纯为数字字符
3:有不合法字符

代码1:简单思路但是繁琐

public class Solution {
    public int StrToInt(String str) {
              //1、三种情况 +123   -123  a123
        if (str == null || str.length () == 0) {
            return 0;
        }
        char ch[] = str.toCharArray ();
        int sum = 0;
        char first = ch[0];
        if (first == '+') {
            for (int i = 1 ; i < ch.length ; i++) {
                char tmp = ch[i];
                if (tmp < '0' || tmp > '9') {
                    return 0;
                }
                sum = sum * 10 + tmp - '0';
            }
        } else if (first == '-') {
            for (int i = 1 ; i < ch.length ; i++) {
                char tmp = ch[i];
                if (tmp < '0' || tmp > '9') {
                    return 0;
                }
                sum = sum * 10 + tmp - '0';
            }
            return sum*-1;
        } else if (first <= '0' || first <= '9') {
            for (int i = 0 ; i < ch.length ; i++) {
                char tmp = ch[i];
                if (tmp < '0' || tmp > '9') {
                    return 0;
                }
                sum = sum * 10 + tmp - '0';
            }
        }else {
            return  0;
        }
        return sum;
    }
}
复制代码

代码2:精简优化版

public class Solution {
    public int StrToInt(String str) {
               char ch[]=str.toCharArray ();
        if(str.isEmpty ()){
            return 0;
        }
        int flg=1;
        if(ch[0]=='-'){
          flg=-1;
          ch[0]='0';
        }else if(ch[0]=='+'){
            flg=1;
            ch[0]='0';
        }
        int sum=0;
        for (int i = 0 ; i <ch.length  ; i++) {
            if(ch[i]<'0'||ch[i]>'9'){
                sum=0;
                break;
            }
            sum=sum*10+ch[i]-'0';
        }
        return  flg*sum;
    }
    
}
复制代码