Q1:创建有三个属性(ID、Name、Balance)的银行账户,并可查余额
/**
* 创建有三个属性(ID、Name、Balance)的银行账户,并可查余额,这是Account账户
*/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;/**
* 创建有三个属性(ID、Name、Balance)的银行账户,并可查余额,这是Bank
*/public class Bank {
public static void main(String[] args) {
HashSet 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
ID>");
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;
/**
* 熟悉HashSet和Collection
*/
public class CollectionTest1 {
public static void main(String[] args) {
Set set1 = new HashSet<>();
set1.add(1);
//set.add("a");
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 c = set1;
Set set2 = new HashSet<>(c);
set2.remove(4);
set2.add(6);
for (int i : set2) {
System.out.print(i + " ");
}
System.out.println();
Set set3 = new HashSet<>(set1);
set3.addAll(set2);
for (int i : set3) {
System.out.print(i + " ");
}
System.out.println();
Set 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 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 sort(ArrayList list) {
ArrayList temp = new ArrayList<>(list);
ArrayList 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 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();
}}
以上就是动力节点java培训机构的小编针对“常见的Java集合编程练习题”的内容进行的回答,希望对大家有所帮助,如有疑问,请在线咨询,有专业老师随时为你服务。
你适合学Java吗?4大专业测评方法
代码逻辑 吸收能力 技术学习能力 综合素质
先测评确定适合在学习