Q1:创建有三个属性(ID、Name、Balance)的银行账户,并可查余额
public class Account {
private long id;
private String name;
private double balance;
public Account(long id, String name, double balance) {
this.id = id;
this.name = name;
this.balance = balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public long getId() {
return this.id;
}
public String getName() {
return this.name;
}
public double getDouble() {
return this.balance;
}
@Override
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof Account)) {
if (((Account)obj).getId() == (this.getId())) {
return true;
}
}
return false;
}
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Balance: " + balance;
}
}
import java.util.Scanner;
import java.util.HashSet;
public class Bank {
public static void main(String[] args) {
HashSet<Account> accountList = new HashSet<>();
accountList.add(new Account(1234567890, "李华", 1000.0));
accountList.add(new Account(1234567891, "嘿嘿", 4000.0));
accountList.add(new Account(1234567892, "王强", 2000.0));
accountList.add(new Account(1234567893, "赵刚", 3000.0));
Scanner scanner = new Scanner(System.in);
System.out.print("请输入待查询的ID\nID>");
String str = scanner.next();
try {
var id = Long.parseLong(str);
for (var account : accountList) {
if (account.getId() == id) {
System.out.println(account);
}
}
} catch (NumberFormatException e) {
System.out.println(e);
}
scanner.close();
}
}
Q2:熟悉HashSet和Collection
import java.util.Collection;
import java.util.Set;
import java.util.HashSet;
public class CollectionTest1 {
public static void main(String[] args) {
Set<Integer> set1 = new HashSet<>();
set1.add(1);
set1.add(5);
set1.add(4);
set1.add(3);
set1.add(2);
set1.add(3);
for (int i : set1) {
System.out.print(i + " ");
}
System.out.println();
Collection<Integer> c = set1;
Set<Integer> set2 = new HashSet<>(c);
set2.remove(4);
set2.add(6);
for (int i : set2) {
System.out.print(i + " ");
}
System.out.println();
Set<Integer> set3 = new HashSet<>(set1);
set3.addAll(set2);
for (int i : set3) {
System.out.print(i + " ");
}
System.out.println();
Set<Integer> set4 = new HashSet<>(set1);
set4.retainAll(set2);
for (int i : set4) {
System.out.print(i + " ");
}
}
}
Q3:从控制台输入若干个单词(输入回车结束)放入集合中,将这些 单词排序后(忽略大小写)打印出来
import java.util.Scanner;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class CollectionTest2 {
public static String getMax(ArrayList<String> list) {
var count = 0;
for (String str1 : list) {
count = 0;
for (String str2 : list) {
if (str1.compareToIgnoreCase(str2) > 0) {
count++;
}
}
if (count == list.size()-1) {
return str1;
}
}
return null;
}
public static ArrayList<String> sort(ArrayList<String> list) {
ArrayList<String> temp = new ArrayList<>(list);
ArrayList<String> result = new ArrayList<>();
String max;
for (int i = 0; i < list.size(); i++) {
max = getMax(temp);
result.add(max);
temp.remove(max);
}
return result;
}
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
var scanner = new Scanner(System.in);
System.out.println("请输入一串文本,用#分割每个字符串,用回车键结束");
var string = scanner.nextLine();
var st = new StringTokenizer(string, "#");
String temp;
while (st.hasMoreTokens()) {
temp = st.nextToken();
list.add(temp);
}
for (String str : sort(list)) {
System.out.println(str);
}
scanner.close();
}
}
Q4:用HashSet盛放学生数据并测试
public class Student {
private String id;
private String name;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return this.id;
}
public String getName() {
return this.name;
}
@Override
public String toString() {
return "ID: " + this.id + " Name: " + this.name;
}
}
import java.util.Set;
import java.util.HashSet;
public class StudentSetTest {
public static void main(String[] args) {
Set<Student> studentSet = new HashSet<>();
Student student1 = new Student("A0001", "王明");
Student student2 = new Student("A0002", "李刚");
Student student3 = new Student("A0003", "赵宇");
studentSet.add(student1);
studentSet.add(student2);
studentSet.add(student3);
for (Student student : studentSet) {
System.out.println(student);
}
}
}
Q5:在一个列表中存储以下元素:apple、grape、banana、pear,返回集合中的最大的和最小的元素,将集合进行排序,并将排序后的结果打印在控制台上
import java.util.ArrayList;
public class ListTest1 {
public static void main(String[] args) {
ArrayList<String> fruitList = new ArrayList<>();
fruitList.add("apple");
fruitList.add("grape");
fruitList.add("banana");
fruitList.add("pear");
System.out.println(getMax(fruitList));
System.out.println(getMin(fruitList));
for (String str : sort(fruitList)) {
System.out.println(str);
}
}
public static String getMax(ArrayList<String> list) {
var count = 0;
for (String str1 : list) {
count = 0;
for (String str2 : list) {
if (str1.compareTo(str2) > 0) {
count++;
}
}
if (count == list.size()-1) {
return str1;
}
}
return null;
}
public static String getMin(ArrayList<String> list) {
var count = 0;
for (String str1 : list) {
count = 0;
for (String str2 : list) {
if (str1.compareTo(str2) < 0) {
count++;
}
}
if (count == list.size()-1) {
return str1;
}
}
return null;
}
public static ArrayList<String> sort(ArrayList<String> list) {
ArrayList<String> temp = new ArrayList<>(list);
ArrayList<String> result = new ArrayList<>();
String max;
for (int i = 0; i < list.size(); i++) {
max = getMax(temp);
result.add(max);
temp.remove(max);
}
return result;
}
}
Q6:用for、foreach、Iterator三种方法分别遍历ArrayList、LinkedList
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
public class ArrayListAndLinkedListTest {
public static void main(String[] args) {
List<Integer> arrayList = new ArrayList<>();
List<Integer> linkedList = new LinkedList<>();
arrayList.add(211);
arrayList.add(100);
arrayList.add(996);
arrayList.add(985);
arrayList.add(798);
arrayList.add(3);
linkedList.addAll(arrayList);
System.out.println("for循环遍历ArrayList:");
for (int i = 0; i < arrayList.size(); i++) {
System.out.print(arrayList.get(i) + " ");
}
System.out.println("\nfor循环遍历LinkedList:");
for (int i = 0; i < linkedList.size(); i++) {
System.out.print(linkedList.get(i) + " ");
}
System.out.println("\nforeach循环遍历ArrayList:");
for (int i : arrayList) {
System.out.print(i + " ");
}
System.out.println("\nforeach循环遍历LinkedList:");
for (int i : linkedList) {
System.out.print(i + " ");
}
Iterator<Integer> arrayIterator = arrayList.iterator();
Iterator<Integer> linkedIterator = linkedList.iterator();
System.out.println("\nIterator遍历ArrayList:");
while (arrayIterator.hasNext()) {
System.out.print(arrayIterator.next() + " ");
}
System.out.println("\nIterator遍历LinkedList:");
while (linkedIterator.hasNext()) {
System.out.print(linkedIterator.next() + " ");
}
}
}
Q7:对集合遍历的修改进行测试
import java.util.*;
public class IteratorTest {
public static void main(String[] args) {
Collection<Integer> c = new ArrayList<>();
c.add(1);
c.add(2);
c.add(3);
c.add(4);
c.add(5);
Iterator<Integer> iterator = c.iterator();
while (iterator.hasNext()) {
int i = iterator.next();
if (i == 3) {
iterator.remove();
}
System.out.println(i);
}
System.out.println();
while (iterator.hasNext()) {
int i = iterator.next();
System.err.println("ss");
System.out.println(i);
}
System.out.println();
for (int i : c) {
System.out.println(i);
}
System.out.println();
Collection<Integer> p =Collections.synchronizedCollection(new ArrayList<>(c));
Iterator<Integer> iterator3 = p.iterator();
while (iterator3.hasNext()) {
int i = iterator3.next();
if (i == 4) {
p.remove(4);
iterator3.remove();
}
}
}
}