1. 输入5个数,装入容器中ArrayList,将数从小到大输出,从大到小输出,随机输出
import java.util.ArrayList;
import java.util.Collections;
public class Test1 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);list.add(2);list.add(3);list.add(56);
Collections.sort(list);//升序;
System.out.println(list);
Collections.reverse(list);//反转;
System.out.println(list);
Collections.shuffle(list);//随机;
System.out.println(list);
}
}
2.int[][] a={{1,2,3},{6,5},{4,10,15}};
请用Collections,ArrayList,进行排序(从大到小)
依次输出(遍历);
package com.xinbo;
import java.util.ArrayList;
import java.util.Collections;
public class Test2 {
/*
* int[][] a={{1,2,3},{6,5},{4,10,15}};
* 请用Collections,ArrayList,进行排序(从大到小)
* 依次输出(遍历);
*/
public static void main(String[] args) {
int[][] a={{1,2,3},{6,5},{4,10,15}};
ArrayList<Integer> list=new ArrayList<>();
for(int[] b:a){
for(int x:b){
list.add(x);
}
}
Collections.sort(list);
Collections.reverse(list);
for(int x:list){
System.out.print(x+" ");
}
}
}
3.请用一个容器把如下用户名:hot,rainbow,hyx,xiaohuang装起来;
再用一个容器把用户名对应的密码:123,1234,12345,159357也装起来;
要求:
请用Scanner输入用户和密码,进行判断;如果输入的用户名和密码是上面所有的,
就输出“判断成功”,否则继续输入,名直到正确为止;
参考答案:
package org.huangyuxin;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
/*
* 1、请用一个容器把如下用户名:hot,rainbow,hyx,xiaohuang装起来;
再用一个容器把用户名对应的密码:123,1234,12345,159357也装起来;
要求:
请用Scanner输入用户名和密码,进行判断;如果输入的用户名和密码是上面所有的,就输出“判断成功”,否则继续输入,直到正确为止;
*/
public static void main(String[] args) {
ArrayList username=new ArrayList();
ArrayList password=new ArrayList();
username.add("hot");username.add("rainbow");
username.add("hyx");username.add("xiaohuang");
password.add("123");password.add("1234");
password.add("12345");password.add("159357");
Scanner s=new Scanner(System.in);
System.out.println("请输入用户名:");
String user=s.nextLine();
System.out.println("请输入密 码:");
String pass=s.nextLine();
a:while(true){
for(int i=0;i<username.size();i++){
if(user.equals(username.get(i)) && pass.equals(password.get(i))){
System.out.println("登录成功");
break a;
}
}
System.out.println("请再一次输入用户名:");
user=s.nextLine();
System.out.println("请再一次输入密 码:");
pass=s.nextLine();
}
}
}
4.int[][] a={{1,2,3},{6,5},{4,10,15}};
用Collections,ArrayList,进行排序(从大到小)
依次输出(遍历);
package com.xinbo.day21;
import java.util.ArrayList;
import java.util.Collections;
public class Test1 {
public static void main(String[] args) {
int[][] a={{1,2,3},{6,5},{4,10,15}};
ArrayList<Integer> list=new ArrayList<>();
for(int[] b:a){
for(int x:b){
list.add(x);
}
}
Collections.sort(list);
Collections.reverse(list);
for(int x:list){
System.out.print(x+" ");
} }}
5.已知有两个容器(List),第一容器中有:[小明,小东,小猪]
第二容器中有:[100分,95分,89分];
(1)请把第二个容器中的100分改成98分;* 8分钟
(2)请用迭代(iterator)来做,在控制台上输出:**(双迭代)
小明:98分;
小东:95分;
小猪:89分;
package com.xinbo.day21;
import java.util.ArrayList;
import java.util.Iterator;
public class Test2{
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("小明");list.add("小东");list.add("小猪");
ArrayList<String> list2 = new ArrayList<>();
list2.add("100分");list2.add("95分");list2.add("89分");
list2.set(0, "98分");
Iterator i=list.iterator();
Iterator ii=list2.iterator();
while(i.hasNext() && ii.hasNext()){
System.out.print(i.next()+":");
System.out.println(ii.next());
}
}
}
6. 请你自编一个用户名和密码的信息,再加上验证码;(4位)
要求:请用HashMap进行封装,用Scanner进行验证(用户名和密码
必须是一一对应)且验证码也必须是正确,才能算“登录成功”
package com.xinbo.day21;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
public class Snippet {
public static void main(String[] args) {
HashMap<String, String> m = new HashMap<>();
m.put("rainbow", "123456");
m.put("huangyuxin", "159357");
m.put("hyx", "753159");
String[] num={"a","b","c","1","2","3","A","B","C"};
Random r=new Random();String pass="";
Scanner s=new Scanner(System.in);
while(true){
for(int i=0;i<4;i++){
int a=r.nextInt(num.length);
pass=pass+num[a];
}
System.out.println("请输入用户名:");
String username=s.nextLine();
System.out.println("请输入密码:");
String password=s.nextLine();
System.out.println("验证码为:"+pass);
System.out.println("请输入正确的验证码:");
String passtrue=s.nextLine();
if(m.containsKey(username)){
if(m.get(username).equals(password)){
if(pass.equalsIgnoreCase(passtrue)){
System.out.println("登录成功");
break;
}else{
System.out.println("验证码有误;");
}
}else{
System.out.println("密码有误;");
}
}else{
System.out.println("用户名有误:");
}
pass="";
}
}
}
7.已知有两个字符串数组类型: *
String[] s1={"苹果","***","绿色"};
String[] s2={"apple","yellow","green"};
(1)请用一个容器把这两个数组装进去,然后在控制台上输出:
{苹果=apple,***=yellow,绿色=green}
(2)
把以上两个数组的元素放在一个自定义的A类中,然后再用容器把它们装起来,再输出;格式跟(1)一样
package com.xinbo.day21;
import java.util.*;
public class Test3 {
public static void main(String[] args) {
String[] s1={"苹果","***","绿色"};
String[] s2={"apple","yellow","green"};
LinkedHashMap<String, String> m = new LinkedHashMap<>();
for(int i=0;i<s1.length;i++){
m.put(s1[i], s2[i]);
}
System.out.println(m);
ArrayList<A> list = new ArrayList<>();
list.add(new A(s1,s2));
System.out.print("{");
for (A a : list) {
System.out.print(a);
}
System.out.print("}");
// System.out.println(list);
}
}
class A{
String[] s1;
String[] s2;
public A(String[] s1,String[] s2){
this.s1=s1;
this.s2=s2;
}
public String f(){
String str="";
for(int i=0;i<s1.length;i++){
if(i==2){
str=str+s1[i]+"="+s2[i];
}else{
str=str+s1[i]+"="+s2[i]+",";
}
}
return str;
}
@Override
public String toString() {
return f();
}
}
本文出自 “爱吉” 博客,谢绝转载!