编写方法:public static List generateNameList(int n);,生成n个字符串的NameList并返回的代码
时间: 2024-05-28 17:09:28 浏览: 59
public static List<String> generateNameList(int n) {
List<String> nameList = new ArrayList<>();
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random random = new Random();
for (int i = 0; i < n; i++) {
StringBuilder name = new StringBuilder();
int length = random.nextInt(10) + 1; // Assume names are between 1 and 10 letters long
for (int j = 0; j < length; j++) {
int index = random.nextInt(characters.length());
name.append(characters.charAt(index));
}
nameList.add(name.toString());
}
return nameList;
}
相关问题
(4)在Util类中编写方法:public static String searchName(List nameList, String name);,根据指定的name返回相应的字符串,如果没有找到返回null。 (5)在Util类中编写方法:public static Student searchStudent(Map stuMap, String name);,根据指定的name返回对应的学生,如果没有找到返回null。 (6)在Util类中编写方法:public static List generateNameList(int n);,生成n个字符串的NameList并返回。 (7)在Util类中编写方法:Map generateStudentMap(int n);,生成包含n个键的的StudentMap并返回。 说明:调用getRandomStudents方法;调用makeStudentMap方法;返回StudentMap。
代码实现如下:
```
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Util {
// (4) 根据指定的name返回相应的字符串,如果没有找到返回null
public static String searchName(List<String> nameList, String name) {
for (String str : nameList) {
if (str.equals(name)) {
return str;
}
}
return null;
}
// (5) 根据指定的name返回对应的学生,如果没有找到返回null
public static Student searchStudent(Map<String, Student> stuMap, String name) {
if (stuMap.containsKey(name)) {
return stuMap.get(name);
}
return null;
}
// (6) 生成n个字符串的NameList并返回
public static List<String> generateNameList(int n) {
List<String> nameList = new ArrayList<>();
for (int i = 0; i < n; i++) {
nameList.add(getRandomName());
}
return nameList;
}
// (7) 生成包含n个键的的StudentMap并返回
public static Map<String, Student> generateStudentMap(int n) {
Map<String, Student> stuMap = new HashMap<>();
for (int i = 0; i < n; i++) {
String name = getRandomName();
int age = getRandomAge();
int score = getRandomScore();
stuMap.put(name, new Student(name, age, score));
}
return stuMap;
}
// 获取随机姓名
private static String getRandomName() {
String[] firstName = {"张", "王", "李", "赵", "孙", "钱", "周", "吴", "郑", "冯"};
String[] lastName = {"明", "强", "华", "飞", "丽", "娜", "洋", "亮", "刚", "峰"};
String name = firstName[(int) (Math.random() * 10)] + lastName[(int) (Math.random() * 10)];
return name;
}
// 获取随机年龄
private static int getRandomAge() {
return (int) (Math.random() * 10) + 15;
}
// 获取随机成绩
private static int getRandomScore() {
return (int) (Math.random() * 50) + 50;
}
}
```
使用中文解释ssm管理系统文件中util文件里PageHelper类解释以下代码package com.util; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; public class PageHelper { public static void getPage(List<?> list, String name, List<String> nameList, List<String> valueList, int pageSize, String number, HttpServletRequest request, String method) { StringBuffer buffer = new StringBuffer(); String name2 = name.substring(0, 1).toUpperCase() + name.substring(1); String path = ""; String action = "getAll" + name2 + ".action"; if (method != null) { action = "query" + name2 + "ByCond.action"; } List<Object> objList = new ArrayList<Object>(); if (nameList != null && valueList != null) { for (int i = 0; i < nameList.size(); i++) { path += "&" + nameList.get(i) + "=" + valueList.get(i); } } int pageNumber = list.size(); int maxPage = pageNumber; if (maxPage % pageSize == 0) { maxPage = maxPage / pageSize; } else { maxPage = maxPage / pageSize + 1; } if (number == null) { number = "0"; } int start = Integer.parseInt(number) * pageSize; int over = (Integer.parseInt(number) + 1) * pageSize; int count = pageNumber - over; if (count <= 0) { over = pageNumber; } for (int i = start; i < over; i++) { Object obj = list.get(i); objList.add(obj); } buffer.append(" 共为"); buffer.append(maxPage); buffer.append("页 共有"); buffer.append(pageNumber); buffer.append("条 当前为第"); buffer.append((Integer.parseInt(number) + 1)); buffer.append("页 "); if ((Integer.parseInt(number) + 1) == 1) { buffer.append("首页"); } else { buffer.append("<a href="" + name + "/" + action + "?number=0" + path + "">首页</a>"); } buffer.append(" "); if ((Integer.parseInt(number) + 1) == 1) { buffer.append("上一页"); } else { buffer.append("<a href="" + name + "/" + action + "?number=" + (Integer.parseInt(number) - 1) + "" + path + "">上一页</a>"); } buffer.append(" "); if (maxPage <= (Integer.parseInt(number) + 1)) { buffer.append("下一页"); } else { buffer.append("<a href="" + name + "/" + action + "?number=" + (Integer.parseInt(number) + 1) + "" + path + "">下一页</a>"); } buffer.append(" "); if (maxPage <= (Integer.parseInt(number) + 1)) { buffer.append("尾页"); } else { buffer.append("<a href="" + name + "/" + action + "?number=" + (maxPage - 1) + "" + path + "">尾页</a>"); } String html = buffer.toString(); request.setAttribute("html", html); request.setAttribute(name + "List", objList); }
该代码是一个用于分页的工具类,主要包含一个静态方法getPage。该方法有以下参数:
- list: 需要分页的数据列表
- name: 数据对应的实体类名称
- nameList: 分页链接的参数名列表
- valueList: 分页链接的参数值列表
- pageSize: 每页显示的数据数量
- number: 当前页码
- request: HTTP请求对象
- method: 查询方法名
该方法的作用是根据传入的参数,生成分页链接,并将生成的链接添加到HTTP请求对象中。在生成分页链接时,会根据传入的参数生成对应的查询字符串,并将其添加到链接中。其中,name2是将实体类名称的首字母大写后的结果。path是请求路径,action是请求的动作。如果传入了method参数,则动作为"query"+name2+"ByCond.action",否则动作为"getAll"+name2+".action"。objList是一个空的对象列表,暂时没有被使用。
阅读全文