This is the 10th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021 “@TOC
š±š
1: Collection classes in Java include ArrayList, LinkedList, HashMap, etc.
A ArrayList and LinkedList implement the List interface B ArrayList access is faster than LinkedList C ArrayList performs better when randomly adding and removing elements D HashMap implement the Map interface, which allows any type of key and value objects
šÆ resolution: ArrayList is an array at the bottom, so deleting elements and randomly adding them doesn’t perform as well as LinkedList.
B) šļø
2: Read the following programs and choose which one is the correct output
class HelloA{
public HelloA(a)
{
System.out.println("Iām A class ");
}
static
{
System.out.println("static A"); }}public class HelloB extends HelloA{
public HelloB(a)
{
System.out.println("I 'm B class");
}
static{
System.out.println("static B");
}
public static void main (String[] args){
newHelloB(); }}Copy the code
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
šÆ resolution: call order; Parent classes take precedence over subclasses, static over non-static. So it should print the static code block of the parent class, then the static code block of the child class, then the parent class constructor, then the child class constructor.
C) šļø
3: The output of executing the following code is ()
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;
returnb; }}}Copy the code
A 10 B 20 C 30 D 40
šÆ parse: finally executes regardless of exceptions, so it should be b+=10, twice. I’m going to return 30
C) šļø
4: The output of the following code is _____
boolean b=true?false:true= =true?false:true;
System.out.println(b);
Copy the code
A true B false C null D Empty string
šÆ : Boolean b =true? šÆ : Boolean b =true? false:true? False :true The second three-wood operator follows the right-to-left principle, true? Boolean b = true? false:false; The result of step 3 is false
B) šļø
š±š
2.1 Converting a string to an integer
Link: HTTPS://www.nowcoder.com/questionTerminal/1277c681251b4372bdef344468e4f2eSource: Niuke.comCopy the code
To convert a string to an integer requires library functions that cannot use string to convert integers. Returns 0 if the value is 0 or the string is not a valid value
Data range: String length: meets advanced levels: space complexity and time complexity
Note: ā Any symbol may appear in the string, and output 0 directly when the symbol other than +/- appears. ā” The string may appear +/- and only at the first part of the string.
The sample1The input"+ 2147483647"The output2147483647
Copy the code
The questions are linked here
3. Divide the discussion into three cases. 1: indicates that the first character starts with a ‘+’ or a ‘-‘. 2: indicates that the character string is a purely numeric character. 3: indicates that there are invalid characters
Code 1: Simple but tedious
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 == The '-') {
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;
}
returnsum; }}Copy the code
Code 2: Streamlined and optimized version
public class Solution {
public int StrToInt(String str) {
char ch[]=str.toCharArray ();
if(str.isEmpty ()){
return 0;
}
int flg=1;
if(ch[0] = =The '-'){
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';
}
returnflg*sum; }}Copy the code