中医网站开发,网站建设加排名要多少,免费软件站,男女做羞羞事试看网站路漫漫其修远兮#xff0c;吾将上下而求索。—— 屈原 目录 第一道题#xff1a;集合的灵活运用 第二道题#xff1a;基础编程能力 第三道题#xff1a; 手写 ArrayList 集合#xff08;模拟实现 ArrayList 核心API#xff09; 第四道题#xff1a;二分查找的应用 第五道… 路漫漫其修远兮吾将上下而求索。—— 屈原 目录 第一道题集合的灵活运用 第二道题基础编程能力 第三道题 手写 ArrayList 集合模拟实现 ArrayList 核心API 第四道题二分查找的应用 第五道题手写单链表模拟实现 LinkedList 集合的核心API 第一道题集合的灵活运用 题目如下 对题目进行分析 可以根据囚犯的编号、所占的位置信息可以封装成一个囚犯类接着就是遍历 100 个人的信息了推荐可以用 for 来遍历。这里要注意的是不能重复出现相同的编号这里可以单独构造一个方法去除重复的编号。对于删除奇数位置上的数据那么新 new 一个集合来接收占位为偶数的元素就好了。这题不算难可以根据题目自己试着敲一敲。 具体代码如下答案肯定是不唯一的答案对了还有逻辑清晰即可 先定义了囚犯类 public class People {private int number;private int location;public People(int number, int location) {this.number number;this.location location;}public People() {}public int getNumber() {return number;}public void setNumber(int number) {this.number number;}public int getLocation() {return location;}public void setLocation(int location) {this.location location;}Overridepublic String toString() {return People{ number number , location location };}
} import java.util.ArrayList;
import java.util.List;
import java.util.Random;public class Text {private static ListPeople peopleLists new ArrayList();public static void main(String[] args) {Random random new Random();for (int i 1; i 100 ; i) {//先判断取到的数是否重复了int number random.nextInt(200)1;if (isRepeat(number)){i--;continue;}else{People people new People(number,i);peopleLists.add(people);}}System.out.println(原先的排位);System.out.println(peopleLists);//除去在位在奇数位置的人直到剩最后一位。//1,2,3,4,5,6,7,8//0,1,2,3,4,5,6,7//可以看出来就是要保留集合中的奇数位while (peopleLists.size() 1){ListPeople temp new ArrayList();for (int i 1; i peopleLists.size(); i2) {temp.add(peopleLists.get(i));}peopleLists temp;}System.out.println(后来的排位);System.out.println(peopleLists.get(0));}private static boolean isRepeat(int number){for (int i 0; i peopleLists.size(); i) {if (peopleLists.get(i).getNumber() number) {return true;}}return false;}
} 第二道题基础编程能力 题目如下 对题目进行分析 先定义出来 User 实体类这里难点在于将 userStrs 变量进行拆分为一个个字符串这里就可以使用 split() 方法了用数组来接收先拆分 #,再用数组接收 :的字符串就拆调用两次 split() 方法即可接着就是对数据的类型处理了其中两个难点就是字符串 id 转变为 long 类型就直接用 Long.valueof() 方法就行了还有一个是转变 LocalDay birthday 用 LocalDay.parse() 来解析字符串。再有就是封装到 Map 集合中对原来的 list 集合遍历再需要用到的是 containsKey() 方法来判断是否重复存在了最后可以将数据放到 map 集合中了。 具体代码如下答案肯定是不唯一的答案对了还有逻辑清晰即可 import java.time.LocalDate;public class User {private Long id;private String gender;private LocalDate birthday;private String name;public User() {}public User(Long id, String gender, LocalDate birthday, String name) {this.id id;this.gender gender;this.birthday birthday;this.name name;}public Long getId() {return id;}public void setId(Long id) {this.id id;}public String getGender() {return gender;}public void setGender(String gender) {this.gender gender;}public LocalDate getBirthday() {return birthday;}public void setBirthday(LocalDate birthday) {this.birthday birthday;}public String getName() {return name;}public void setName(String name) {this.name name;}Overridepublic String toString() {return User{ id id , gender gender \ , birthday birthday , name name \ };}
} import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class Text {public static void main(String[] args) {ListUser userLists new ArrayList();String userStrs 10001:张三:男:1990-01-01# 10002:李四:女:1989-01-09# 10003:王五:男:1999-09-09# 10004:刘备:男:1899-01-01# 10005:孙悟空:男:1900-01-01# 10006:张三:女:1999-01-01# 10007:刘备:女:1999-01-01# 10008:张三:女:2003-07-01# 10009:猪八戒:男:1900-01-01#;String[] userInformation userStrs.split(#);for (int i 0; i userInformation.length; i) {String[] userSpilt userInformation[i].split(:);String idString userSpilt[0];long id Long.valueOf(idString);String name userSpilt[1];String gender userSpilt[2];String birthdayString userSpilt[3];LocalDate birthday LocalDate.parse(birthdayString);User user new User(id,gender,birthday,name);userLists.add(user);}System.out.println(userLists);//遍历集合中的每个名字出现的次数MapString,Integer map new HashMap();for (User userList : userLists) {if (map.containsKey(userList.getName())){map.put(userList.getName(),map.get(userList.getName())1);}else {map.put(userList.getName(),1);}}//遍历打印map集合map.forEach((k,v)- System.out.println(k : v));}
} 第三道题 手写 ArrayList 集合模拟实现 ArrayList 核心API 题目如下 对题目进行分析 ArrayList 添加第一个元素时集合默认的大小为数组空间为 10 每当等于或者超过相关的值就会扩容为原来的1.5倍一直往后下去。重点在于一开始类中应该得设置一个 size 0 这个成员变量很重要即是代表了元素的个数还可以是指向下一个应该添加元素的位置。 具体代码如下答案肯定是不唯一的答案对了还有逻辑清晰即可 public interface MyConsumerE {void accept(E e);
} import java.util.Arrays;
public class MyArrayList E{private int defaultLength 10;private Object[] arr new Object[defaultLength];private int size 0;public MyArrayList() {}//添加数据public boolean add(E e){//先判断arr数组是否要扩容if (isExpansion()){//已经扩容成功System.out.println(已经扩容了);}arr[size] e;return true;}//查询数据public E get(int index){if(index size || index 0){throw new RuntimeException();}else {return (E)arr[index];}}//删除数据public E remove(int index) {if (index size || index 0) {throw new RuntimeException();} else {E retainData (E) arr[index];if (index ! size - 1) {//1,2,3,4,5,6//0,1,2,3,4,5//int remainder size - index - 1;for (int i index; i size - 1; i) {arr[i] arr[i 1];}}size--;return retainData;}}//获取集合大小sizepublic int size(){return size;}//开发一个forEach方法public void forEach(MyConsumer myConsumer){for (int i 0; i size; i) {myConsumer.accept(arr[i]);}}private boolean isExpansion(){if (size defaultLength){defaultLength (int) (defaultLength * (1.5));Object[] temp new Object[defaultLength];for (int i 0; i size; i) {temp[i] arr[i];}arr temp;return true;}else {return false;}}Overridepublic String toString() {Object[] temp new Object[size];for (int i 0; i size; i) {temp[i] arr[i];}return Arrays.toString(temp);}
} public class Text {public static void main(String[] args) {MyArrayListString myArrayList new MyArrayList();myArrayList.add(1);myArrayList.add(2);myArrayList.add(3);myArrayList.add(4);myArrayList.add(5);myArrayList.add(6);myArrayList.add(7);myArrayList.add(8);myArrayList.add(9);myArrayList.add(10);myArrayList.add(11);myArrayList.add(13);myArrayList.add(13);myArrayList.add(19);myArrayList.add(13);myArrayList.add(18);System.out.println(myArrayList);System.out.println(------------------);System.out.println(myArrayList.get(2));System.out.println(------------------);System.out.println(myArrayList.remove(3));System.out.println(删除的结果myArrayList);System.out.println(myArrayList.size());System.out.println(---------遍历---------);myArrayList.forEach(System.out::println);}
}第四道题二分查找的应用 题目如下 对题目进行分析 这里注明了必须是确保程序的时间复杂度是 O(log2n)很显然就是要使用二分法来进行查找元素用二分法来寻找目标元素的开始位置还有结束位置那就可以用分别使用二分法来寻找开始位置还有结束位置注意的是若数组中不存在目标元素的话就要返回-1。 具体代码如下答案肯定是不唯一的答案对了还有逻辑清晰即可 import java.util.Arrays;
public class BinaryLookup {public static void main(String[] args) {int[] arr {7,7,7,8,8,8,10};int target 9;int[] a lookupRightAndLeft(arr,target);System.out.println(Arrays.toString(a));}public static int lookupLeft(int[] arr, int target){int left 0;int right arr.length-1;int isLeft - 1;while (left right){int index (leftright)/2;if ( arr[index] target){isLeft index;right index - 1;} else if (arr[index] target) {right index - 1;}else {left index 1;}}return isLeft;}public static int lookupRight(int[] arr, int target){int left 0;int right arr.length-1;int isRight - 1;while (left right){int index (leftright)/2;if ( arr[index] target){isRight index;left index 1;} else if (arr[index] target) {right index - 1;}else {left index 1;}}return isRight;}public static int[] lookupRightAndLeft(int[] arr,int target){int[] returnArr new int[2];int isLeft lookupLeft(arr,target);int isRight lookupRight(arr,target);returnArr[0] isLeft;returnArr[1] isRight;return returnArr;}
} 第五道题手写单链表模拟实现 LinkedList 集合的核心API 题目如下 对题目进行分析 由于官方的 LinkedList 集合使用了内部类来实现的所以为了保持一致我们也使用内部类来模拟实现单链表需要一个个节点来组成因此定义一个内部类来封装节点节点无非就是数据还有该类型对象的引用需要注意的是这里需要设置泛型类我感觉用以上的题目给的信息来实现 LinkedList 集合与原 LinkedList 集合差别会很大而且很乱所以我做了一些改进。 具体代码如下答案肯定是不唯一的答案对了还有逻辑清晰即可 public interface MyConsumerE {void accept(E e);
} public class MyLinkedListE {private int size 0;private NodeCode hand;class NodeCode{E data;NodeCode nextNodeCode;public NodeCode(E data, NodeCode nextNodeCode) {this.data data;this.nextNodeCode nextNodeCode;}}public NodeCode add(E e){if (hand null){hand new NodeCode(e,null);}else {NodeCode temp hand;while (temp.nextNodeCode ! null){temp temp.nextNodeCode;}temp.nextNodeCode new NodeCode(e,null);}size;return hand;}public void forEach(MyConsumerE myConsumer){NodeCode first hand;while ( first!null ){myConsumer.accept(first.data);first first.nextNodeCode;}}public void reverse(int left,int right){NodeCode first hand;int count 1;NodeCode tempLeft null;Object[] arr new Object[right-left 1];while (count right){if (count left){tempLeft first;}if (count left ) {arr[count-left] first.data;}first first.nextNodeCode;count;/* if (count right){arr[count-left] first.data;}*/}for (int i arr.length - 1; i 0; i--) {tempLeft.data (E) arr[i];tempLeft tempLeft.nextNodeCode;}}
} public class Text {public static void main(String[] args) {MyLinkedListInteger myLinkedList new MyLinkedList();myLinkedList.add(1);myLinkedList.add(2);myLinkedList.add(3);myLinkedList.add(4);myLinkedList.add(5);myLinkedList.add(6);myLinkedList.reverse(2,5);myLinkedList.forEach(new MyConsumerInteger() {Overridepublic void accept(Integer integer) {System.out.println(integer);}});/* MyLinkedListString myLinkedList new MyLinkedList();myLinkedList.add(1);myLinkedList.add(2);myLinkedList.add(3);myLinkedList.add(4);myLinkedList.add(5);myLinkedList.add(5);myLinkedList.add(6);myLinkedList.add(6);myLinkedList.forEach(System.out::println);*//* System.out.println(-----------------);LinkedListString list new LinkedList();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);list.add(5);list.add(6);list.forEach(new ConsumerString() {Overridepublic void accept(String s) {System.out.println(s);}});*/}
} 若可以完成大部分题目很高心衷心地祝贺你你的编程能力是极高的 想要进一步了解更多的相关 Java 知识可以点击以下链接小扳_-CSDN博客