最后
光给面试题不给答案不是我的风格。这里面的面试题也只是凤毛麟角,还有答案的话会极大的增加文章的篇幅,减少文章的可读性
Java面试宝典2021版
最常见Java面试题解析(2021最新版)
2021企业Java面试题精选
| E set(int index, E element) | 将下标 index 位置元素设置为 element |
| void clear() | 清空 |
| boolean contains(Object o) | 判断 o 是否在线性表中 |
| int indexOf(Object o) | 返回第一个 o 所在下标 |
| int lastIndexOf(Object o) | 返回最后一个 o 的下标 |
| List subList(int fromIndex, int toIndex) | 截取部分 list |
public static void main(String[] args) {
ArrayList arrayList = new ArrayList<>();
System.out.println(“==尾插e=”);
arrayList.add(“a”);
arrayList.add(“b”);
arrayList.add(“c”);
System.out.println(arrayList);
System.out.println(“==将 e 插入到 index 位置=”);
arrayList.add(0,“qwe”);
System.out.println(arrayList);
System.out.println(“==尾插 c 中的元素=”);
ArrayList arrayList1 = new ArrayList<>();
arrayList1.add(“!!!”);
arrayList.addAll(arrayList1);
System.out.println(arrayList);
System.out.println(“==删除 index 位置元素=”);
System.out.println(arrayList.remove(0));
System.out.println(arrayList);
System.out.println(“==删除遇到的第一个 o=”);
System.out.println(arrayList.remove(“a”));
System.out.println(arrayList);
System.out.println(“==获取下标 index 位置元素=”);
System.out.println(arrayList.get(1));
System.out.println(“==将下标 index 位置元素设置为 element=”);
System.out.println(“原来要修改的数据:”+arrayList.set(1,“d”));
System.out.println(arrayList);
System.out.println(“==判断 o 是否在线性表中=”);
System.out.println(arrayList.contains(“!!!”));
System.out.println(“==返回第一个 o 所在下标=”);
System.out.println(arrayList.indexOf(“!!!”));
System.out.println(“==返回最后一个 o 的下标=”);
System.out.println(arrayList.lastIndexOf(“!!!”));
System.out.println(“==截取部分 list=”);
List sub = arrayList.subList(0,2);//左闭右开
System.out.println(sub);
System.out.println(“==情况=”);
arrayList.clear();
System.out.println(arrayList);
}
运行结果:
**结论:
如果ArrayList调用无参的构造方法new ArrayList() , 那么顺序表的大小是0.当第一次add的时候, 整个顺序表才变为了10;
当这10个放满之后,才开始扩容,以1.5倍的方式扩容.
如果调用的是给定容量的构造方法new ArrayList(13) , 那么顺序表的大小就是给定容量的大小,如果放满了,还是以1.5倍进行扩容.**
=====================================================================================
import java.util.ArrayList;
class Student{
private String name;
private String classes;
private double score;
public Student(String name, String classes, double score) {
this.name = name;
this.classes = classes;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return “Student{” +
“name='” + name + ‘’’ +
“, classes='” + classes + ‘’’ +
“, score=” + score +
‘}’;
}
}
public class TestDemo10 {
public static void main1(String[] args) {
ArrayList students = new ArrayList<>();
students.add(new Student(“niubi”,“102-1”,98.9));
students.add(new Student(“21e”,“123”,22.2));
students.add(new Student(“qwq”,“wqqe”,455.4));
System.out.println(students);
}
}
运行结果:
public static void main(String[] args) {
ArrayList integers = new ArrayList<>();
integers.add(11);
integers.add(2);
integers.add(13);
Collections.sort(integers);
System.out.println(integers);
}
运行结果:
public static void main(String[] args) {
String str1 = “welcome to bit”;
String str2 = “come”;
ArrayList list1 = new ArrayList<>();
for (int i = 0; i < str1.length(); i++) {
char ch = str1.charAt(i);
if(!str2.contains(ch+“”)){
list1.add(ch);
}
}
for (char ch : list1 ) {
System.out.print(ch);
}
}
运行结果:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Card{
private int rank;
private String suit;
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
@Override
public String toString() {
return “{” + suit + “->” +rank + “}”;
}
}
public class Test {
private static final String[] suits = {“❤”,“♠”,“♣”,“♦”};
/**
-
构造一副牌
-
@return
*/
public static List buyCard(){
ArrayList cards = new ArrayList<>();
//四种花色
for (int i = 0; i < 4; i++) {
//每个花色13张牌
for (int j = 0; j < 13; j++) {
/*String suit = suits[i];//花色
int rank = j ;//牌数
Card card = new Card(rank,suit);//得到一张牌card
cards.add(card);*/
cards.add(new Card(j,suits[i]));//优化后的代码
}
}
return cards;
}
private static void swap(List cards,int i,int j){
Card tmp = cards.get(i);
cards.set(i,cards.get(j));
cards.set(j,tmp);
}
/**
-
洗牌
-
@param cards
*/
public static void shuffle(List cards){
int size = cards.size();
for (int i = size - 1; i > 0; i–) {
Random random = new Random();
int rand = random.nextInt(i);
swap(cards,i,rand);
}
}
public static void main(String[] args) {
List cards = buyCard();
System.out.println(“刚开始的牌:”+cards);
shuffle(cards);
System.out.println(“洗过后的牌:”+cards);
ArrayList<List> hand = new ArrayList<>();
List hand1 = new ArrayList<>();
List hand2 = new ArrayList<>();
List hand3 = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
//每个人,轮流揭牌
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
Card card = cards.remove(0);//拿一张牌 少一张牌 remov去掉
hand.get(j).add(card);
}
}
System.out.println("第1个人的牌: "+hand1);
System.out.println("第2个人的牌: "+hand2);
System.out.println("第3个人的牌: "+hand3);
System.out.println("剩余的排: "+cards);
}
}
=====================================================================================
package demo;
import java.util.Arrays;
class MyArrayList {
private Object[] elem;//数组
private int usedSize;//代表有效数据个数
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
public MyArrayList() {
this.elem = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public MyArrayList(int capacity) {
//对参数进行一个判断
if (capacity > 0) {
this.elem = new Object[capacity];
} else if (capacity == 0) {
this.elem = new Object[0];
} else {
throw new IllegalArgumentException(“不能为负数!”);
}
}
/**
-
尾插 e
-
@param e
-
@return
*/
public boolean add(E e) {
//缺点一个真正的容量.扩容
ensureCapacityInternal(usedSize + 1);
elem[usedSize] = e;
usedSize++;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
//计算出需要的容量
int capacity = calculateCapacity(elem, minCapacity);
//拿着计算出的容量,去判断是否扩容,需要扩容就扩容
ensureExplicitCapacity(capacity);
}
private static int calculateCapacity(Object[] elem, int minCapacity) {
//1.是否elem数组分配过大小
if (elem == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(10, minCapacity);
}
//2.分配过 就返回+1的值
return minCapacity;
}
private void ensureExplicitCapacity(int minCapacity) {
if (minCapacity - elem.length > 0) {
//扩容
grow(minCapacity);
}
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private void grow(int minCapacity) {
int oldCapacity = elem.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);//1.5倍扩容
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
//说明需要的容量非常大
newCapacity = hugeCapacity(minCapacity);
elem = Arrays.copyOf(elem, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0)
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
/**
-
将 e 插入到 index 位置
-
@param index
-
@param e
*/
public void add(int index, E e) {
rangeCheckForAdd(index);
ensureCapacityInternal(usedSize + 1);
copy(index, e);
usedSize++;
}
private void copy(int index, E e) {
for (int i = usedSize - 1; i >= index; i–) {
elem[i + 1] = elem[i];
}
elem[index] = e;
}
/**
-
判断index位置是否合法
-
@param index
*/
public void rangeCheckForAdd(int index) {
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException(“index位置不合法无法插入”);
}
}
/**
-
获取顺序表的大小
-
@return
*/
public int size() {
return this.usedSize;
}
/**
-
获取o第一次出现的位置
-
@param o
-
@return
*/
public int indexOf(Object o) {
if (null == o) {
for (int i = 0; i < usedSize; i++) {
if (elem[i] == null) {
return i;
}
}
} else {
for (int i = 0; i < usedSize; i++) {
if (elem[i].equals(o)) {
return i;
}
}
}
return -1;
}
/**
-
删出第一次出现的e
-
@param o
-
@return
*/
public boolean remove(Object o) {
int index = indexOf(o);
if (index == -1) {
return false;
}
remove(index);
return true;
}
/**
-
删除index位置上元素
-
@param index
-
@return
*/
public E remove(int index) {
rangeCheckForAdd(index);
E e = (E) elem[index];
// 将index之后的元素统一往前搬移一个位置
for (int i = index; i < usedSize - 1; ++i) {
elem[i] = elem[i + 1];
}
elem[usedSize] = null;
usedSize–;
return e;
}
/**
-
获取index位置上的元素
-
@param index
-
@return
*/
public E get(int index) {
rangeCheckForAdd(index);
return (E) elem[index];
总结
其他的内容都可以按照路线图里面整理出来的知识点逐一去熟悉,学习,消化,不建议你去看书学习,最好是多看一些视频,把不懂地方反复看,学习了一节视频内容第二天一定要去复习,并总结成思维导图,形成树状知识网络结构,方便日后复习。
这里还有一份很不错的《Java基础核心总结笔记》,特意跟大家分享出来
目录:
部分内容截图:
urn true;
}
/**
-
删除index位置上元素
-
@param index
-
@return
*/
public E remove(int index) {
rangeCheckForAdd(index);
E e = (E) elem[index];
// 将index之后的元素统一往前搬移一个位置
for (int i = index; i < usedSize - 1; ++i) {
elem[i] = elem[i + 1];
}
elem[usedSize] = null;
usedSize–;
return e;
}
/**
-
获取index位置上的元素
-
@param index
-
@return
*/
public E get(int index) {
rangeCheckForAdd(index);
return (E) elem[index];
总结
其他的内容都可以按照路线图里面整理出来的知识点逐一去熟悉,学习,消化,不建议你去看书学习,最好是多看一些视频,把不懂地方反复看,学习了一节视频内容第二天一定要去复习,并总结成思维导图,形成树状知识网络结构,方便日后复习。
这里还有一份很不错的《Java基础核心总结笔记》,特意跟大家分享出来
目录:
[外链图片转存中…(img-dYJZV63K-1715605738604)]
部分内容截图:
[外链图片转存中…(img-3LagPfnu-1715605738605)]
[外链图片转存中…(img-qrHzHDO2-1715605738605)]