[英]Storing/Tracking arraylist objects to table elements
Q: How do I keep track of which ArrayList
element belongs to which element in a JTable
for when I later have to put it all together into one single ArrayList
? 问:当我以后不得不将所有
ArrayList
元素归为一个单独的ArrayList
时,如何跟踪该元素属于JTable
哪个元素?
DETAILED: 详细:
I have an interface Media
, and 3 classes implementing it: Book
, Movie
, Album
. 我有一个
Media
接口,以及3个实现它的类: Book
, Movie
, Album
。 Album
has an ArrayList
of Songs
(also a class I made, obviously). Album
有一个ArrayList
of Songs
(显然也是我制作的一个类)。
I made a JTable
as per usual with Object[][]
where I can put in String
s, int
s and such for the time being and the user can add rows, remove rows, edit the cells, and then later I planned on going through the data and making each thing specified in the JTable
and putting them into an ArrayList
called ListOne of type Media
so I can use ObjectOutputStream
and save the List
(yes, this is a demand in the assignment, I would've kept it simple personally). 我使用
Object[][]
像往常一样制作了一个JTable
,暂时可以在其中放入String
, int
,用户可以添加行,删除行,编辑单元格,然后我计划继续通过数据,使在指定的每一件事情JTable
,并把它们放入一个ArrayList
称为那么listOne类型的Media
,所以我可以使用ObjectOutputStream
并保存List
(是的,这是在分配的需求,我已经把它简单的个人)。
But the problem is Songs
are an ArrayList
in the class Album
, and so I made another JFrame
with a JTable
where one can similarly fill that one out, and then I thought I would store all of the songs the user provides into a temporary Album
-object. 但是问题是
Songs
是Album
类中的ArrayList
,所以我用JTable
制作了另一个JFrame
,在其中可以类似地填充该JTable
,然后我想我会将用户提供的所有歌曲存储到一个临时Album
-宾语。 But where do I go from here? 但是我从这里去哪里呢? I can't keep track of them like this if the user deletes the corresponding
Album
in the first JTable
. 如果用户在第一个
JTable
删除相应的Album
,我将无法跟踪它们。
If I store these Album
s in an ArrayList
called ListTwo , and if the user decides to delete no.6 in ListOne , and no.6 happens to be an Album
, I have to know which one to delete in ListTwo now and make sure the others' ID or key isn't affected as a result because I will need to pair them properly later. 如果我将这些
Album
存储在名为ListTwo的ArrayList
,并且如果用户决定删除ListOne中的 6号,而6号恰好是Album
,则我必须知道现在要删除ListTwo中的哪一个,并确保因此,其他人的ID或密钥不会受到影响,因为稍后需要正确配对。 I need to know which Album
's songs in ListTwo to transfer to which of the Album
s in ListOne so I can save it later. 我需要知道哪些
Album
的在ListTwo歌曲转移到其的Album
S IN 那么listOne,所以我可以稍后保存。
So how would I go about keeping track of this and connecting them later? 那么我将如何跟踪并稍后将它们连接起来?
Preliminary code: 初步代码:
Media interface class: 媒体接口类:
public interface Media {
void setAuthor(String author);
String getAuthor();
}
Book class: 书本类:
public class Book implements Media {
private String author;
Book(){
this.author = "";
}
@Override
public void setAuthor(String author) {
this.author = author;
}
@Override
public String getAuthor() {
return this.author;
}
}
CD(Album) class: CD(专辑)类
import java.util.ArrayList;
public class CD implements Media {
public class Song {
private int seconds;
private String songName;
private String artist;
Song(String name, int seconds, String artist){
this.songName = name;
this.seconds = seconds;
this.artist = artist;
}
public String getName(){
return this.songName;
}
public void setName(String name){
this.songName = name;
}
}
private ArrayList<Song> songs;
private String artist;
CD(){
songs = new ArrayList<Song>();
this.artist = "";
}
public void addSong(String name, int seconds, String artist){
this.songs.add(new Song(name, seconds, artist));
}
public void removeSong(int position){
this.songs.remove(position);
}
@Override
public void setAuthor(String author){
this.artist = author;
}
@Override
public String getAuthor(){
return this.artist;
}
}
Movie class: 电影课:
public class Movie implements Media {
private String producer;
Movie(){
this.producer = "";
}
@Override
public void setAuthor(String author){
this.producer = author;
}
@Override
public String getAuthor(){
return this.producer;
}
}
TABLE: http://pastebin.com/ETJGuQhD 表格: http : //pastebin.com/ETJGuQhD
My solution was to make 3 Add-buttons instead of one - one for each type of object that can be added to the final ArrayList. 我的解决方案是制作3个“添加”按钮,而不是一个-每个可以添加到最终ArrayList的对象类型一个。 Then I made three separate add-functions, one for each button, and in each I not only add a row with a specific type already chosen, I also add an object of that type directly to the final ArrayList.
然后,我制作了三个单独的添加函数,每个按钮一个,并且在每个函数中,我不仅添加了已经选择的特定类型的行,而且还将该类型的对象直接添加到最终的ArrayList中。 Then I use the table.getRowSelection() as part of the createTable-function to pick out which one in the ArrayList to alter when I finish the list of Songs.
然后,我将table.getRowSelection()用作createTable函数的一部分,以在完成歌曲列表时选择要更改的ArrayList中的哪一个。 After which the songs are directly stored in the Album object pointed out.
之后将歌曲直接存储在指出的专辑对象中。
This way I can be sure that everything is in the right place, even though it is kinda clunky. 这样,即使有些笨拙,我也可以确保一切都在正确的位置。 But I'd rather finish on time than not finish on time.
但是我宁愿准时完成而不愿按时完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.