gpt4 book ai didi

java初始化对象数组

转载 作者:行者123 更新时间:2023-12-02 03:05:20 25 4
gpt4 key购买 nike

//这是我的第一篇文章,如果我做错了什么,请告诉我。 :D

我最近开始“学习”Java,并且刚刚开始编写一些 csgo 皮肤管理器之类的东西。我想为游戏中实现的每个皮肤添加一个对象,所以我从一个对象数组开始。问题是游戏中有大约 608 个皮肤,但我不想打字if(i+1 == [ID]){weapons = new Skins("weaponName", "skinName", "randomQuality", "garbageCollection", i+1)}600+次,有没有更快的办法?

编辑:我在 .ods 文件中获取了所有信息,因此“问题”实际上与“代码结构”有关,而不是初始化本身

//这是与您相关的两个类:

    package cs.skins;
public class Main{
private static final int NUMBER_OF_SKINS = 608;

private Skins[] weapons;

public Main(){
weapons = new Skins[NUMBER_OF_SKINS];
initSkins();
}

private void initSkins(){
for(int i = 0; i < weapons.length; i++){

if(i+1 == 1){
weapons[i] = new Skins();
}else if(i+1 == 2){
weapons[i] = new Skins();
}
}
}
}

和:

    package cs.skins;

public class Skins {

private String weapon;
private String skin;
private String quality;
private String collection;
private int id;
private int numberOwned;


public Skins(String weapon, String skin, String quality, String collection, int id){
this.weapon = weapon;
this.skin = skin;
this.quality = quality;
this.collection = collection;
this.id = id;
this.numberOwned = 0;
}

public String getWeapon() {
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
public String getSkin() {
return skin;
}
public void setSkin(String skin) {
this.skin = skin;
}
public String getQuality() {
return quality;
}
public void setQuality(String quality) {
this.quality = quality;
}
public String getCollection() {
return collection;
}
public void setCollection(String collection) {
this.collection = collection;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNumberOwned() {
return numberOwned;
}
public void setNumberOwned(int numberOwned) {
this.numberOwned = numberOwned;
}
}

最佳答案

但在此之前您需要弄清楚如何解析 ods。 (我建议转换为其他更简单的格式)

private void initSkins(){
for(int i = 0; i < weapons.length; i++){
weapons[i] = new Skins("weapon name", "skin name", "quality", "collection", 3);
}
}

关于java初始化对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41838570/

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