// 定义一个方法run(),可以让很多汽车一起进入参加比赛 public static void run(ArrayList<?> list){ }
时间: 2024-04-10 15:35:07 浏览: 14
你可以使用以下代码定义一个方法 `run()`,它接受一个 `ArrayList` 参数,让多辆汽车一起参加比赛:
```java
public static void run(ArrayList<?> list) {
// 在这里编写比赛的逻辑
}
```
在方法体内,你可以编写比赛的具体逻辑,例如计算每辆汽车的速度、比较它们的排名等。注意,`ArrayList<?>` 使用了通配符,表示这个列表可以接受任何类型的元素。你可以根据实际需要将其替换为具体的汽车类或其他类型。
相关问题
(Remove duplicates) Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList<Integer> list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers in their input order and separated by exactly one space. Here is a sample run: Enter 10 integers: 34 5 3 5 6 4 33 2 2 4 The distinct integers are 34 5 3 6 4 33 2
Here's the implementation of the `removeDuplicate` method:
```java
public static void removeDuplicate(ArrayList<Integer> list) {
ArrayList<Integer> distinctList = new ArrayList<Integer>();
for (Integer num : list) {
if (!distinctList.contains(num)) {
distinctList.add(num);
}
}
list.clear();
list.addAll(distinctList);
}
```
And here's the test program:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<Integer>();
System.out.print("Enter 10 integers: ");
for (int i = 0; i < 10; i++) {
int num = input.nextInt();
list.add(num);
}
removeDuplicate(list);
System.out.print("The distinct integers are ");
for (Integer num : list) {
System.out.print(num + " ");
}
}
public static void removeDuplicate(ArrayList<Integer> list) {
ArrayList<Integer> distinctList = new ArrayList<Integer>();
for (Integer num : list) {
if (!distinctList.contains(num)) {
distinctList.add(num);
}
}
list.clear();
list.addAll(distinctList);
}
}
```
Sample output:
```
Enter 10 integers: 34 5 3 5 6 4 33 2 2 4
The distinct integers are 34 5 3 6 4 33 2
```
根据以下要求:Instead of using a binary file to save the arraylist of points, change the savaData method and the constructor of the Model class to use a database to write / read the coordinates of all the points. Use XAMPP and phpMyAdmin to create a database called "java" with a table called "points" that has two integer columns x and y (in addition to the ID primary key). Hint: make sure you delete all the old point coordinates from the database before inserting new ones. Hint: use phpMyAdmin to check what is stored in the database. Use the Test class to run all the tests for the software and check that all the tests still work. Use the Start class to run the software and check that closing the software correctly saves the point coordinates in the database (use phpMyAdmin to check the content of the database). Run the software again and check that all the points from the previous run are correctly displayed,修改下述代码:public class Model implements Serializable { private ArrayList<Point> points; private ArrayList<ModelListener> listeners; private static final String FILE_NAME = "points.bin"; public Model() { points = new ArrayList<Point>(); listeners = new ArrayList<ModelListener>(); // Read points from file if it exists File file = new File(FILE_NAME); if (file.exists()) { try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); points = (ArrayList<Point>) in.readObject(); in.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } public void addListener(ModelListener l) { listeners.add(l); } public ArrayList<Point> getPoints() { return points; } public void addPoint(Point p) { points.add(p); notifyListeners(); // points changed so notify the listeners. saveData(); // save point to file } public void clearAllPoints() { points.clear(); notifyListeners(); // points changed so notify the listeners. saveData(); // save empty list to file } public void deleteLastPoint() { if (points.size() > 0) { points.remove(points.size() - 1); notifyListeners(); // points changed so notify the listeners. saveData(); // save updated list to file } } private void notifyListeners() { for (ModelListener l : listeners) { l.update(); // Tell the listener that something changed. } } public int numberOfPoints() { return points.size(); } public void saveData() { try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE_NAME)); out.writeObject(points); out.close(); } catch (IOException e) { e.printStackTrace(); } }
public class Model {
private ArrayList<Point> points;
private ArrayList<ModelListener> listeners;
private static final String DB_URL = "jdbc:mysql://localhost/java";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "";
public Model() {
points = new ArrayList<Point>();
listeners = new ArrayList<ModelListener>();
// Read points from database
try {
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM points";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int x = rs.getInt("x");
int y = rs.getInt("y");
Point p = new Point(x, y);
points.add(p);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void addListener(ModelListener l) {
listeners.add(l);
}
public ArrayList<Point> getPoints() {
return points;
}
public void addPoint(Point p) {
points.add(p);
notifyListeners(); // points changed so notify the listeners.
saveData(); // save point to database
}
public void clearAllPoints() {
points.clear();
notifyListeners(); // points changed so notify the listeners.
saveData(); // save empty list to database
}
public void deleteLastPoint() {
if (points.size() > 0) {
points.remove(points.size() - 1);
notifyListeners(); // points changed so notify the listeners.
saveData(); // save updated list to database
}
}
private void notifyListeners() {
for (ModelListener l : listeners) {
l.update(); // Tell the listener that something changed.
}
}
public int numberOfPoints() {
return points.size();
}
public void saveData() {
try {
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
String sql = "DELETE FROM points";
stmt.executeUpdate(sql);
for (Point p : points) {
sql = "INSERT INTO points (x, y) VALUES (" + p.x + ", " + p.y + ")";
stmt.executeUpdate(sql);
}
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)