使用Java在

时间:2016-03-23 19:24:23

标签: java

因此,基本上,I' M目前具有我的程序,其中,我想创建对象的数组列表,并具有放置在由用户的值的问题,我'已经尚未去​​有关如何这样做。

在我的班级,我已经创造了这些属性。

List<Games> games = new ArrayList<Games>();
    List<Company> company = new ArrayList<Company>();
    int userchoice;

在games.java类中,我使用类似的属性来接收数据。

games.java

public List<Games> getTitles() {
    return titles;
}


public void setTitles(List<Games> titles) {
    this.titles = titles;
}

我会使用games.add(gameTitle);来放置值,但它会给我2个错误。

当我使用incompatible types: String cannot be converted into Games

时,其中一个会告诉我games.add(1, gamesTitle);

接下来会告诉我

method collection.Add(Games) is not applicable, String cannot be converted to Games.

我尝试做的是将用户值从gameID和游戏Title中获取到一个arrayList中,然后将该数据作为参数发送到类并将它们放入数据库中。

这是我正在处理的Java Persistence API项目,我们将不胜感激。谢谢

Games.java Class

@Entity (name = "Games")
public class Games implements Serializable {
@Id 
private int id;
private String title;
private String genre;

@ManyToOne
@JoinColumn(name="GamesComp")

private Company comp_Name;


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 String getGenre() {
    return genre;
}


public void setGenre(String genre) {
    this.genre = genre;
}


public Company getComp_Name() {
    return comp_Name;
}


public void setComp_Name(Company comp_Name) {
    this.comp_Name = comp_Name;
}

@Override
public String toString() {
    return "Game ID: " + getId() + " Title: " + getTitle() + 
           " with " + getGenre();
}

公司类

@Entity
@Table (name="Company")
public class Company implements Serializable {
@Id 
private int id;
private String compName;
@OneToMany(cascade = ALL, mappedBy="comp")   

private List<Games> titles;

public Company() {
    titles = new ArrayList<>();
}


public int getId() {
    return id;
}


public void setId(int id) {
    this.id = id;
}


public String getCompName() {
    return compName;
}


public void setCompName(String compName) {
    this.compName = compName;
}

public void addGameTitles(Games t) {
    this.titles.add(t);
    t.setComp_Name(this);

}


public List<Games> getTitles() {
    return titles;
}


public void setTitles(List<Games> titles) {
    this.titles = titles;
}

@Override
public String toString() {
    return "Company ID: " + getId() + 
           ", Company Name: " + getCompName();
}    

1 个答案:

答案 0 :(得分:0)

问题是:您正在尝试将字符串对象插入游戏对象列表...

替换此List<Games> games = new ArrayList<Games>();

为此:

List<String> games = new ArrayList<String>();

你当然可以定义像

这样的游戏
class Games{
...
Games(String gameTitle){
}

以及代码中的某些地方将游戏添加到游戏列表中:

games.add(new Games("CallOfDuty"));
imho,最后一个选项更优雅,反映出更好的设计模式..

但你有选择的自由。