gpt4 book ai didi

java - 如何按名称删除 arraylist Room 中的房间?

转载 作者:行者123 更新时间:2023-12-02 11:56:23 25 4
gpt4 key购买 nike

我正在学习java。我在我的 java 程序中遇到了有关 arraylist 的问题。如何删除 Room 中 arraylist 中的房间并计算大小小于 40 的房间数量。我刚刚在 MyRoom 类中编写了 2 个方法:removeRoom 和 countRoomBySize。我的代码在 Net Beans 中出现错误。请任何人帮我检查一下这段代码。非常感谢。这是我的代码 我的主课

public class MyMain { 
public static void main(String[] args) {
//create a list of rooms
MyRoom m = new MyRoom();

m.addRoom(new Room("HB201L",35));
m.addRoom(new Room("HB401R",45));
m.addRoom(new Room("211",30));

m.sort();
m.list();

//1.
m.removeRoom("211");
m.list();


//2.
int c = m.countRoomBySize(40);
System.out.println(c);//2
}
}

教室

public class Room implements Comparable<Room> {
@Override
public int compareTo(Room o) {
return o.name.compareToIgnoreCase(this.name);
}
//instanced variables
private int size;
private String name;

public Room() {
name = "";
size = 0;
}

public Room(String name, int size) {
this.name = name;
this.size = size;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

MyRoom 类

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class MyRoom implements IRoom {

List<Room> rooms;

public MyRoom() {
rooms = new ArrayList();
}

@Override
public void addRoom(Room r) {
//append r to the end of list rooms
rooms.add(r);
}

@Override
public void list() {
for (int i = 0; i < rooms.size(); i++) {
Room r = rooms.get(i);
System.out.printf("%-20s%-10d\n",r.getName(),r.getSize());
}
}

@Override
public void removeRoom(){
for (int i = 0; i < rooms.size(); i++) {
if(rooms.get(i).getName() == "211")
rooms.remove(rooms);
}
}

@Override
public int countRoomBySize(){
int s,i,n;
n = rooms.size();
s = 0;
for(i=0;i<n;i++) {
if(rooms.get(i).getSize() > 40)
s++;
}
return(s);
}

public void sort() {
Collections.sort(rooms);
}
}

Interface IRoom
public interface IRoom {
//only contain public members: constants and method declaration
public final int MAX = 10;
public void addRoom(Room r);
public void list();
public void removeRoom();
public int countRoomBySize();
}
interface A {
void f();
}
interface B extends A {
void g();
}
interface C {}

最佳答案

首先像这样更改你的界面 IRoom :

public void removeRoom(String name);
public int countRoomBySize(int size);

在 MyRoom 类中更改 removeRoom 方法,如下所示:

@Override
public void removeRoom(String name){
for (int i = 0; i < rooms.size(); i++) {
if(name.equalsIgnoreCase(rooms.get(i).getName())){
rooms.removeIf(r -> r.getName().equals(name));
System.out.println("Removed !");
}
}
}

countRoomBySize 方法如下:

@Override
public int countRoomBySize( int size){
int s,i,n;
n = rooms.size();
s = 0;
for(i=0;i<n;i++) {
if(rooms.get(i).getSize() > size)
s++;
}
return(s);
}

祝你好运!

关于java - 如何按名称删除 arraylist Room 中的房间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47575676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com