我正在尝试添加从我的数组列表中删除对象的函数(首先我要为这些对象生成唯一的随机ID:
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Random;
public class Medium {
public Medium(){
this.id = generateID();
System.out.println("ID: " + id);
mlist.add(this);
}
protected int generateID() {
Random random = new Random();
int id;
do {
id = 100 + (random.nextInt(999-100));
} while(idlist.contains(id));
idlist.add(id);
return id;
}
protected boolean delete(Medium delElem) {
boolean isDeleted = false;
ListIterator<Medium> it = mlist.listIterator();
while(it.hasNext()) {
it = mlist.listIterator();
Medium next = it.next();
if(delElem.getID() == next.getID()) {
delElem = next;
mlist.remove(delElem);
delElem = null;
if(delElem == null) {
System.out.println("Succesfull deleted");
}
isDeleted = true;
break;
}
}
return isDeleted;
}
protected int getID() {
return id;
}
public String getTitle() {
return title;
}
protected ArrayList<Medium> mlist = new ArrayList<Medium>();
private ArrayList<Integer> idlist = new ArrayList<Integer>();
protected String title;
protected final int id;
}
我不确定我是否理解这一点,但是如果我设置delElem =满足相同id的对象,删除我的对象并设置它= null,则该对象不应再引用。但是如果我在我的main函数中测试它,例如book.getTitle(),则会打印标题。我认为这是因为它只是我函数中的本地对象。如何在函数中全局删除我的对象并在成功删除对象时返回布尔值?
谢谢!
答案 0 :(得分:1)
评论中已经说明您通常不会手动删除对象(通过将它们设置为null)。垃圾收集将在不再需要时完成它的工作。
代码中的奇怪之处在于媒体将自己添加到列表中。为什么不创建另一个代表书架的课程。这样您就可以在书架上实现添加或删除书籍的方法:
package com.example;
import java.util.ArrayList;
import java.util.Random;
public class BookShelf {
protected ArrayList<Medium> mlist = new ArrayList<Medium>();
private ArrayList<Integer> idlist = new ArrayList<Integer>();
private class Medium {
protected int id;
protected String title;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
public Medium addMedium(String title) {
Medium medium = new Medium();
medium.setId(this.generateID());
medium.setTitle(title);
this.mlist.add(medium);
return medium;
}
protected int generateID() {
Random random = new Random();
int id;
do {
id = 100 + (random.nextInt(999 - 100));
} while (idlist.contains(id));
idlist.add(id);
return id;
}
protected boolean delete(Medium delElem) {
boolean isDeleted = false;
for (Medium medium : mlist) {
if (medium.getId() == delElem.getId()) {
mlist.remove(medium);
isDeleted = true;
break;
}
}
return isDeleted;
}
public ArrayList<Medium> getAllBooks() {
return this.mlist;
}
@Override
public String toString() {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("Books in the shelf: " + this.getAllBooks().size() + "\n");
for(Medium medium : this.getAllBooks()) {
strBuilder.append("Title: " + medium.getTitle() + "\n");
}
return strBuilder.toString();
}
public static void main(String[] args) {
BookShelf bookShelf = new BookShelf();
//Add two books to the shelf
Medium medium1 = bookShelf.addMedium("Book 1");
Medium medium2 = bookShelf.addMedium("Book 2");
System.out.println(bookShelf.toString());
//Delete one
bookShelf.delete(medium1);
System.out.println(bookShelf.toString());
}
}