目录
1. this()构造方法的调用,super()父类成员变量或方法的调用
2. 浮点型 和 long 不能做 switch() 的参数类型
3. .java源文件中,只能有一个和文件名相同的public类,可以包含其他类
4. ConcurrentHashMap使用通过volatile修饰符和后插入后特殊的算法实现读不加锁.
10. 构造方法可以重载,子类可以通过super调用父类构造方法编辑
1. this()构造方法的调用,super()父类成员变量或方法的调用
this的作用,在构造方法中可以调用其他构造方法,this()
super的作用,在子类中访问父类的成员变量和成员方法 super()
这道题问的是构造方法调用,选B
2. 浮点型 和 long 不能做 switch() 的参数类型
switch的括号内只能是以下类型的表达式:
基本类型:byte,cahr,short,int,注意不能是long和浮点类型
引用类型:String常量串,枚举类型
3. .java源文件中,只能有一个和文件名相同的public类,可以包含其他类
.java源文件中,可以有很多个类,但这些类中,必须要有一个和文件名相同并且被public修饰的类
4. ConcurrentHashMap使用通过volatile修饰符和后插入后特殊的算法实现读不加锁.
ConcurrentHashMap使用通过volatile修饰符和后插入后特殊的算法实现读不加锁.
HashMap实现了Map接口
Array.asList返回的ArrayList不是util包中的ArrayList,而是Arrays类的一个继承了AbstractList内部类
5. final表示属性的不可变及方法的不可重写
7. 统计回文
题目链接:统计回文_牛客题霸_牛客网 (nowcoder.com)
题目要求:
题目分析:
上代码
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str1 = scan.nextLine();
String str2 = scan.nextLine();
//1.找到插入位置
int len = str1.length();
int count = 0;
for(int i = 0; i <= len; i++) {
StringBuilder sb = new StringBuilder(str1);
sb.insert(i,str2);
//反转会改变原字符串,所以这里再拷贝一份原字符串
StringBuilder sb1 = new StringBuilder(sb);
StringBuilder sb2 = sb1.reverse();
//2.判断回文
//StringBuilder中没有重写equals方法,所以要转为String
if(sb.toString().equals(sb2.toString())) {
count++;
}
}
System.out.println(count);
}
}
8. 连续最大和
题目链接:连续最大和_牛客题霸_牛客网 (nowcoder.com)
题目分析:
状态方程式:max(dp[i]) = getMax(max(dp[i-1]) + arr[i],arr[i])
dp[i]就是以数组下标为i的数做为结尾的最大子序列和
用状态方程式求得两种情况的最大者sum,然后先和max = arr[0],比较如果sum大,就把sum传给max,一直这样循环,直到走到最后求得最大连续子树组和max
上代码
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
private static int getMax(int a, int b) {
return a > b ? a : b;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] array = new int[n];
for(int i = 0; i < n; i++) {
array[i] = scan.nextInt();
}
int sum = array[0];
int max = array[0];
for(int i = 0; i < n; i++) {
sum = getMax(sum+array[i],array[i]);
if(sum > max) {
max = sum;
}
}
System.out.println(max);
}
}
9. 查找组成一个偶数最接近的两个素数
题目链接:查找组成一个偶数最接近的两个素数_牛客题霸_牛客网 (nowcoder.com)
题目要求:
题目分析:
上代码
package 改;
import java.util.Scanner;
public class day12_2_查找组成一个偶数最接近的两个素数 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNextInt()) {
int n = scan.nextInt();
for(int i = n/2; i > 0; i--) {
if(isPrime(i) && isPrime(n-i)) {
System.out.println(i);
System.out.println(n-i);
break;
}
}
}
}
private static boolean isPrime(int a) {
for(int i = 2; i < a; i++) {
if(a%i == 0) {
return false;
}
}
return true;
}
}
10. 构造方法可以重载,子类可以通过super调用父类构造方法
java语言中构造方法可以重载,并且可以通过new来自动调用构造方法,也可以在子类通过super调用父类的构造方法,所以CD错误
11. 数据类型的转换
首先,明确java中八进制和十六进制的表示方式,
表示八进制,前面加0,如int a = 012;
表示十六进制,前面加0x或者0X,如 int a = 0x23;
A选项是表示8进制的数,test = 10。10是int类型,test是long,小赋值给大,自动类型转换,A对
B选是 f 是 float类型的,-412是int类型的,小赋值给大的,自动类型转换,B对
C选项时 true是boolean类型的,不能强制类型转换为int,C错
D选,0x12345678是十六进制数,用double来接收还是发生自动类型转换D对
E,byte占一个字节,表示范围是-128-127,E选项这里给b赋值了128,超过这个范围了,E错
12. 三目运算符套三目运算符的计算顺序,从后往前
13. HashMap使用链地址法解决哈希冲突
上面的四种都是解决哈希冲突的方法,但是
在JDK1.6,JDK1.7中,HashMap采用位桶+链表实现,即使用链表处理冲突,
而JDK1.8中,HashMap采用位桶+链表+红黑树实现,当链表长度超过阈值(8)时,将链表转换为红黑树,这样大大减少了查找时间,但是也是使用链地址法。
所以总的来说选C
14. 把字符串转换成整数
题目链接:把字符串转换成整数_牛客题霸_牛客网 (nowcoder.com)
题目要求:
题目分析:
上代码
public class Solution {
public int StrToInt(String str) {
if(str == null || str.isEmpty()) {
return 0;
}
int flag = 1;
int sum = 0;
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) >= '0' && str.charAt(i) <= '9') {
sum = sum*10 + (str.charAt(i) - '0');
}else if(str.charAt(0) == '-') {
flag = -1;
}else if(str.charAt(0) == '+') {
flag = 1;
}else {
return 0;
}
}
sum = sum*flag;
return sum;
}
}
15. 不要二
题目链接:不要二_牛客题霸_牛客网 (nowcoder.com)
题目要求:
题目分析:
上代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int w = in.nextInt();
int h = in.nextInt();
int[][] array = new int[w][h];
int count = 0;
for(int i = 0; i < w; i++) {
for(int j = 0; j < h; j++) {
//当arr[i][j]为0时才可以放蛋糕count++,为1时不可以
if(array[i][j] == 0) {
count++;
if(j+2 < h) {
array[i][j+2] = 1;
}
if(i+2 < w) {
array[i+2][j] = 1;
}
}
}
}
System.out.println(count);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/91217.html