生成一个String的ArrayList对象
时间: 2024-05-01 10:20:47 浏览: 72
可以使用以下代码创建一个包含多个字符串的ArrayList对象:
```java
ArrayList<String> strings = new ArrayList<String>();
strings.add("Hello");
strings.add("World");
strings.add("!");
```
这将创建一个名为`strings`的ArrayList对象,并将三个字符串添加到其中。
相关问题
然后调用PageManager.getPages(url,4,sql, request ),返回一个ArrayList的对象,在for循环里,使用jsp得到每个ArrayList对象的数据
在这个代码片段中,`PageManager.getPages(url, 4, sql, request)` 是一个方法调用,它接收四个参数:URL、每页显示的数量(可能是分页参数)、SQL查询字符串以及一个request对象。这个方法很可能是从某个页面管理类(PageManager)中获取数据,比如从数据库中获取信息,然后按照指定数量分页。
`getPages` 返回的是一个 `ArrayList` 对象,这意味着它是一个动态数组,可以存储多个同类对象。在 `for` 循环中,每次迭代都会遍历这个 `ArrayList` 的内容。对于每个 `ArrayList` 中的元素,你可以通过 JSP (JavaServer Pages) 来访问其属性并将其数据显示到网页上。JSP允许你在服务器端动态生成HTML内容,并结合Java代码操作数据。
举个例子,假设 `ArrayList` 存储的是 `Page` 类型的对象,那么在 JSP 中的代码可能会像这样:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
List<Page> pages = (List<Page>) request.getAttribute("pages");
for (Page p : pages) {
String title = p.getTitle(); // 获取页面标题
String content = p.getContent(); // 获取页面内容
out.println("<h2>" + title + "</h2>");
out.println(content);
}
%>
```
声明一个 Student 类,属性包括姓名、学号、总成绩;生成 10 个 Student 类对象,并放在一个一维数组中,编写方法按总成绩进行排序,将排序后的对象 分别保持在Vector 、ArrayList、 HashTable类型的对象中,并遍历显示其中 元素的信息。 程序设计参考框架如下: import java,util.Vector; import java.util.ArrayList; import java.util.Hashtable; import java.util.Enumeration; import java.util.Iterator; public class Ex71 { public static void main (String[] args){
public class Student implements Comparable<Student>{
private String name;
private int id;
private int totalScore;
public Student(String name, int id, int totalScore){
this.name = name;
this.id = id;
this.totalScore = totalScore;
}
public String getName(){
return name;
}
public int getId(){
return id;
}
public int getTotalScore(){
return totalScore;
}
public void setName(String name){
this.name = name;
}
public void setId(int id){
this.id = id;
}
public void setTotalScore(int totalScore){
this.totalScore = totalScore;
}
public int compareTo(Student s){
return this.totalScore - s.totalScore;
}
}
import java.util.Vector;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Iterator;
public class Ex71 {
public static void main (String[] args){
Student[] students = new Student[10];
students[0] = new Student("Tom", 1001, 80);
students[1] = new Student("Jerry", 1002, 90);
students[2] = new Student("Alice", 1003, 85);
students[3] = new Student("Bob", 1004, 70);
students[4] = new Student("Peter", 1005, 95);
students[5] = new Student("Mary", 1006, 65);
students[6] = new Student("John", 1007, 75);
students[7] = new Student("Lucy", 1008, 88);
students[8] = new Student("David", 1009, 92);
students[9] = new Student("Lily", 1010, 78);
// sort by total score
Arrays.sort(students);
// put sorted objects into Vector
Vector<Student> vector = new Vector<Student>();
for(Student s : students){
vector.add(s);
}
// put sorted objects into ArrayList
ArrayList<Student> arrayList = new ArrayList<Student>();
for(Student s : students){
arrayList.add(s);
}
// put sorted objects into Hashtable
Hashtable<Integer, Student> hashtable = new Hashtable<Integer, Student>();
int i = 1;
for(Student s : students){
hashtable.put(i++, s);
}
// display elements in Vector
System.out.println("Elements in Vector:");
Enumeration<Student> enumeration = vector.elements();
while(enumeration.hasMoreElements()){
Student s = enumeration.nextElement();
System.out.println("Name: " + s.getName() + ", ID: " + s.getId() + ", Total Score: " + s.getTotalScore());
}
// display elements in ArrayList
System.out.println("Elements in ArrayList:");
Iterator<Student> iterator = arrayList.iterator();
while(iterator.hasNext()){
Student s = iterator.next();
System.out.println("Name: " + s.getName() + ", ID: " + s.getId() + ", Total Score: " + s.getTotalScore());
}
// display elements in Hashtable
System.out.println("Elements in Hashtable:");
Enumeration<Integer> keys = hashtable.keys();
while(keys.hasMoreElements()){
Integer key = keys.nextElement();
Student s = hashtable.get(key);
System.out.println("Name: " + s.getName() + ", ID: " + s.getId() + ", Total Score: " + s.getTotalScore());
}
}
}
阅读全文