java集合测试和代码 java测试类代码
各位大神高分求一个JAVA集合问题,请给出详细的代码
import java.util.ArrayList;
十载的汤原网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整汤原建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“汤原网站设计”,“汤原网站推广”以来,每个客户项目都认真落实执行。
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static String str = "[{TIME=2006-06, PRICE=246.91388889, AccountMethod=7, BW_VALUE=88888}, {TIME=2006-07, PRICE=305611.11111111, AccountMethod=1, BW_VALUE=7070120}, {TIME=2006-08, PRICE=246.91388889, AccountMethod=1, BW_VALUE=88888}, {TIME=2006-08, PRICE=246.91388889, AccountMethod=7, BW_VALUE=7777}]";
public static ListDemo demos = new ArrayListDemo();
public static MapString, Demo maps = new HashMapString, Demo();
public static Pattern p = Pattern.compile("\\{.*?\\}");
public static Pattern p_time = Pattern.compile("TIME=.*?,");
public static Pattern p_price = Pattern.compile("PRICE=.*?,");
public static Pattern p_account = Pattern.compile("AccountMethod=.*?,");
public static Pattern p_value = Pattern.compile("BW_VALUE=.*?\\}");
public static void main(String[] args) {
Matcher m = p.matcher(str);
while (m.find()) {
Demo demo = null;
String s = m.group();
String t = null;
Matcher m2 = null;
m2 = p_time.matcher(s);
if (m2.find()) {
t = m2.group();
t = t.substring(5, t.length() - 1);
if (maps.containsKey(t)) {
demo = maps.get(t);
m2 = p_price.matcher(s);
if (m2.find()) {
String p = m2.group();
demo.setPRICE(demo.getPRICE() + "+"
+ p.substring(6, p.length() - 1));
}
m2 = p_account.matcher(s);
if (m2.find()) {
String a = m2.group();
a = a.substring(14, a.length() - 1);
if (a.equals("7")) {
m2 = p_value.matcher(s);
if (m2.find()) {
String v = m2.group();
v = v.substring(9, v.length() - 1);
demo.setFree(Double.parseDouble(v)
+ demo.getFree());
}
} else {
m2 = p_value.matcher(s);
if (m2.find()) {
String v = m2.group();
v = v.substring(9, v.length() - 1);
demo.setCharge(Double.parseDouble(v)
+ demo.getCharge());
}
}
}
} else {
demo = new Demo();
demo.setTIME(t);
m2 = p_price.matcher(s);
if (m2.find()) {
String p = m2.group();
demo.setPRICE(p.substring(6, p.length() - 1));
}
m2 = p_account.matcher(s);
if (m2.find()) {
String a = m2.group();
a = a.substring(14, a.length() - 1);
if (a.equals("7")) {
m2 = p_value.matcher(s);
if (m2.find()) {
String v = m2.group();
v = v.substring(9, v.length() - 1);
demo.setFree(Double.parseDouble(v));
}
} else {
m2 = p_value.matcher(s);
if (m2.find()) {
String v = m2.group();
v = v.substring(9, v.length() - 1);
demo.setCharge(Double.parseDouble(v));
}
}
}
}
}
maps.put(t, demo);
}
System.out.println(getResult());
}
public static String getResult() {
StringBuffer sb = new StringBuffer();
sb.append("[");
IteratorString iter = maps.keySet().iterator();
while (iter.hasNext()) {
String key = iter.next();
Demo demo = maps.get(key);
String temp = null;
if (demo.getFree() != 0) {
if (demo.getCharge() != 0) {
temp = "{TIME=" + demo.getTIME() + ",PRICE="
+ demo.getPRICE() + ",charge=" + demo.getCharge()
+ ",free=" + demo.getFree() + "}";
} else {
temp = "{TIME=" + demo.getTIME() + ",PRICE="
+ demo.getPRICE() + ",free=" + demo.getFree() + "}";
}
} else {
if (demo.getCharge() != 0) {
temp = "{TIME=" + demo.getTIME() + ",PRICE="
+ demo.getPRICE() + ",charge=" + demo.getCharge()
+ "}";
} else {
temp = "{TIME=" + demo.getTIME() + ",PRICE="
+ demo.getPRICE() + "}";
}
}
sb.append(temp + ",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append("]");
return sb.toString();
}
}
其中的Demo就是一个javabean。
编辑一个Java问题测试程序怎么写
是控制台输入吗。为了方便类都写一个里面了。可以参考下
建立一个题目实体(标题、4个选项属性、正确答案);
建立一个结果实体(标题、是否正确结果、选择的答案、正确答案);
建立一个题目list集合,用于存放题目(每个难度对应一个list);
建立一个结果list集合,用于存放作答后的信息;
创建对应的题目对象,实体化实体,并且放入对应的list里面
根据选择对应的难度找到对应难度的list集合;
然后遍历这个集合,依次显示每个题目的信息,并让用户作答,答完一个显示下一个题目,
获取用户选择的答案,用这个答案跟实体中的正确答案做比较;
创建结果实体对象,将比较后的信息set到实体中。(标题、正确结果、选择的答案、正确答案);
将这个结果实体放入到结果list集合中;循环结束后遍历结果list集合显示就可以了。
代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class SaveLog {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 提示信息
System.out.println("请输入数字选择题目难度,1:简单,2:困难");
// 获取到对应难度的题目
ListQuestion questionList = getQuestionByLevel(in.nextInt());
// 创建结果list
ListQuestionResult questionResults = new ArrayListQuestionResult();
// 总答题,正确,错误
int questionCount = questionList.size();
int correntCount = 0;
int errorCount = 0;
// 遍历题目
for (int i = 0; i questionList.size(); i++) {
// 获取到题目
Question question = questionList.get(i);
System.out.println("题目" + (i + 1) + ":" + question.getTitle());
// 获取到题目选项并打印控制台
ListString selected = question.getList();
for (String string : selected) {
System.out.println(string);
}
// 获取到选择的答案
String corrent = in.next();
// 获取到做题结果
QuestionResult qr = setQuestionResult(question, corrent);
// 计算错误与正确的题目
if (corrent.equalsIgnoreCase(question.getCorrentResult())) {
correntCount = correntCount + 1;
} else {
errorCount = errorCount + 1;
}
// 放入结果集合
questionResults.add(qr);
}
System.out.println("您一共答了" + questionCount + "道题,其中正确的为" + correntCount + "题,错误为" + errorCount + "题");
System.out.println();
// 遍历结果
for (QuestionResult questionResult : questionResults) {
if (!questionResult.getSelected().equals(questionResult.getCorrentResult())) {
System.out.println("错误题目:" + questionResult.getTitle());
System.out.println("是否正确:" + questionResult.getResult());
System.out.println("选择的答案:" + questionResult.getSelected() + ",正确答案:" + questionResult.getCorrentResult());
System.out.println();
}
}
}
public static QuestionResult setQuestionResult(Question question, String corrent) {
QuestionResult result = new QuestionResult();
result.setCorrentResult(question.getCorrentResult());
result.setTitle(question.getTitle());
result.setSelected(corrent);
// 如果答案正确
if (corrent.equalsIgnoreCase(question.getCorrentResult())) {
result.setResult("正确");
} else {
result.setResult("错误");
}
return result;
}
/** 根据难度获取对应题目(1:简单。2:困难) */
public static ListQuestion getQuestionByLevel(int level) {
ListQuestion result = null;
if (level == 1) {
// 创建list集合用于存放题目,简单难度
ListQuestion questionList = new ArrayListQuestion();
// 添加题目到list
Question question = new Question();
question.setTitle("中国文化博大精深,请问1+2等于几?");
// 题目答案选项
ListString list = new ArrayListString();
list.add("A:1");
list.add("B:2");
list.add("C:3");
list.add("D:6");
question.setCorrentResult("C");
question.setList(list);
// 将题目放到list集合
questionList.add(question);
// 设置返回结果
result = questionList;
} else if (level == 2) {
// 创建list集合用于存放题目,简单难度
ListQuestion questionList = new ArrayListQuestion();
// 添加题目到list
Question question = new Question();
question.setTitle("朝辞白帝彩云间的下一句是什么?");
// 题目答案选项
ListString list = new ArrayListString();
list.add("A:千里江陵一日还");
list.add("B:野火烧不尽");
list.add("C:离离原上草");
list.add("D:智商捉鸡");
question.setCorrentResult("A");
question.setList(list);
// 将题目放到list集合
questionList.add(question);
// 设置返回结果
result = questionList;
}
return result;
}
}
/** 题目结果类 */
class QuestionResult {
/** 题目名称 */
private String title;
/** 是否正确 */
private String result;
/** 选择的答案 */
private String selected;
/** 正确答案 */
private String correntResult;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getSelected() {
return selected;
}
public void setSelected(String selected) {
this.selected = selected;
}
public String getCorrentResult() {
return correntResult;
}
public void setCorrentResult(String correntResult) {
this.correntResult = correntResult;
}
}
/** 题目类 */
class Question {
/** 题目名称 */
private String title;
/** 题目的所有选项 */
private ListString list;
/** 正确答案 */
private String correntResult;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public ListString getList() {
return list;
}
public void setList(ListString list) {
this.list = list;
}
public String getCorrentResult() {
return correntResult;
}
public void setCorrentResult(String correntResult) {
this.correntResult = correntResult;
}
}
JAVA集合题怎么做求代码
ListObject employeeList = new LinkedList();
employeeList.add(xxx);
Java里面如何求两个集合的交集
用Collection提供的retainAll方法,如下是测试代码:
import java.util.*;
public class TestList1{
public static void main(String[] args){
List list1=new ArrayList();
list1.add("A");list1.add("B");list1.add("C");
List list2=new ArrayList();
list2.add("B");list2.add("C");list2.add("D");
list1.retainAll(list2);
System.out.println(“交集为:”+list1);
}
}
//测试:
//交集为:[B, C]
名称栏目:java集合测试和代码 java测试类代码
URL分享:http://azwzsj.com/article/dodhicj.html