英文:
select array of class objects field
问题
我有一个枚举和一个带有两个字段的类:
enum types {
    nan,
    num,
    text,
    col,
    dress
  };
class GeneType {
  String name;
  types type;
  GeneType(String name, types type) {
    this.name = name;
    this.type = type;
  }
}
我还有一个使用这个类的对象数组:
final GeneType[][] geneNames = new GeneType[][]{
  {
    new GeneType("Unknown", types.nan),
    new GeneType("Age", types.num),
    new GeneType("Fav number", types.num),
    new GeneType("Height", types.num),
    new GeneType("Name", types.text)
  },
  {
    new GeneType("Unknown", types.nan),
    new GeneType("Not Sure", types.nan),
    new GeneType("This", types.nan),
    new GeneType("That", types.nan),
    new GeneType("The other", types.nan),
    new GeneType("Eye color", types.col),
    new GeneType("Fav color", types.col),
    new GeneType("Name", types.text)
  }
}
我想要使用最简单的方法来选择一组名称数组以设置选项卡名称(String[]):
new Tabs(x, y, wid, hei, orientation, thingsToShow,
 geneNames[howMuchDataNeed(thingsToShow.size())].name)
其中,thingsToShow 是另一个类的 ArrayList。
如何选择 geneNames[index].name 数组?
英文:
I have an enum and a class with two fields:
enum types {
    nan,
    num,
    text,
    col,
    dress
  };
class GeneType {
  String name;
  types type;
  GeneType(String name, types type) {
    this.name = name;
    this.type = type;
  }
}
I have also array of objects that are using this class
final GeneType[][] geneNames = new GeneType[][]{
  {
    new GeneType("Unknown", types.nan),
    new GeneType("Age", types.num),
    new GeneType("Fav number", types.num),
    new GeneType("Height", types.num),
    new GeneType("Name", types.text)
  },
  {
    new GeneType("Unknown", types.nan),
    new GeneType("Not Sure", types.nan),
    new GeneType("This", types.nan),
    new GeneType("That", types.nan),
    new GeneType("The other", types.nan),
    new GeneType("Eye color", types.col),
    new GeneType("Fav color", types.col),
    new GeneType("Name", types.text)
  }
}
I wand to select array of names using the simplest method to set tab names (String[])
new Tabs(x, y, wid, hei, orientation, thingsToShow,
 geneNames[howMuchDataNeed(thingsToShow.size())].name)
thingsToShow is arrayList of otherClass
How to select array of geneNames[index].name?
答案1
得分: 2
从geneNames[index]中,您将根据您提供的index值获取内部数组之一。例如,如果调用geneNames[0],您将获得以下数组作为结果。
[
    new GeneType("Unknown", types.nan),
    new GeneType("Age", types.num),
    new GeneType("Fav number", types.num),
    new GeneType("Height", types.num),
    new GeneType("Name", types.text)
]
然后,您需要将这个GeneType对象数组映射到它们对应名称的数组。为此,您可以使用Java的stream API,如下所示。
String[] names = Arrays.stream(geneNames[index])
        .map(gene -> gene.name)
        .toArray(String[]::new);
更新: 不使用Java stream API实现
GeneType[] selected = geneNames[index];
String[] names = new String[selected.length];
for (int i = 0; i < selected.length; i++) {
    names[i] = selected[i].name;
}
英文:
From geneNames[index] you will get one of the internal arrays based on the index value you have provided. For example, if geneNames[0] is called, you will get the following array as the result.
[
    new GeneType("Unknown", types.nan),
    new GeneType("Age", types.num),
    new GeneType("Fav number", types.num),
    new GeneType("Height", types.num),
    new GeneType("Name", types.text)
]
Then what you require is to map this array of GeneType objects to an array of their corresponding names. For that, you can use Java's stream APIs as follows.
String[] names = Arrays.stream(geneNames[index])
        .map(gene -> gene.name)
        .toArray(String[]::new);
Update: Implementation without using Java stream APIs
GeneType[] selected = geneNames[index];
String[] names = new String[selected.length];
for (int i = 0; i < selected.length; i++) {
    names[i] = selected[i].name;
}
答案2
得分: 1
尝试一下。
int index = 1;
String[] names = Arrays.stream(geneNames[index])
    .map(gene -> gene.name)
    .toArray(String[]::new);
System.out.println(Arrays.toString(names));
输出:
[Unknown, Not Sure, This, That, The other, Eye color, Fav color, Name]
英文:
Try this.
int index = 1;
String[] names = Arrays.stream(geneNames[index])
    .map(gene -> gene.name)
    .toArray(String[]::new);
System.out.println(Arrays.toString(names));
output:
[Unknown, Not Sure, This, That, The other, Eye color, Fav color, Name]
答案3
得分: 0
以下是翻译好的部分:
可以通过在GeneType类中重写toString()方法来实现这一点。
我希望这段代码能对你有所帮助。
import java.util.ArrayList;
import java.util.Arrays;
enum types {
    nan,
    num,
    text,
    col,
    dress
};
class GeneType {
    String name;
    types type;
    GeneType(String name, types type) {
        this.name = name;
        this.type = type;
    }
    @Override
    public String toString() {
        return name;
    }
}
class question {
    public static void main(String[] args) {
        final GeneType[][] geneNames = new GeneType[][]{
            {
                new GeneType("Unknown", types.nan),
                new GeneType("Age", types.num),
                new GeneType("Fav number", types.num),
                new GeneType("Height", types.num),
                new GeneType("Name", types.text)
            },
            {
                new GeneType("Unknown", types.nan),
                new GeneType("Not Sure", types.nan),
                new GeneType("This", types.nan),
                new GeneType("That", types.nan),
                new GeneType("The other", types.nan),
                new GeneType("Eye color", types.col),
                new GeneType("Fav color", types.col),
                new GeneType("Name", types.text)
            }
        };
        ArrayList<GeneType> names = new ArrayList<>();
        for (int i = 0; i < geneNames[0].length; i++) {
            names.add(geneNames[0][i]);
        }
        System.out.println(names);
    }
}
英文:
You can do this using toString() overriding method in GeneType class.
I hope this code will help you.
import java.util.ArrayList;
import java.util.Arrays;
enum types {
nan,
num,
text,
col,
dress
};
class GeneType {
String name;
types type;
GeneType(String name, types type) {
this.name = name;
this.type = type;
}
@Override
public String toString() {
return name;
}
}
class question {
public static void main(String[] args) {
final GeneType[][] geneNames = new GeneType[][]{
{
new GeneType("Unknown", types.nan),
new GeneType("Age", types.num),
new GeneType("Fav number", types.num),
new GeneType("Height", types.num),
new GeneType("Name", types.text)
},
{
new GeneType("Unknown", types.nan),
new GeneType("Not Sure", types.nan),
new GeneType("This", types.nan),
new GeneType("That", types.nan),
new GeneType("The other", types.nan),
new GeneType("Eye color", types.col),
new GeneType("Fav color", types.col),
new GeneType("Name", types.text)
}
};
ArrayList<GeneType> names = new ArrayList<>();
for (int i = 0; i < geneNames[0].length; i++) {
names.add(geneNames[0][i]);
}
System.out.println(names);
}
}
答案4
得分: 0
这可能不是最佳选项,但它能够通过。
String[] SelectNames(){
  GeneType[] carry = geneNames[index];
  String[] toReturn = new String[carry.length];
  for(int i=0; i<toReturn.length; i++)
    toReturn[i]=carry[i].name;
  return toReturn;
}
英文:
This isn't propably the best option, but it pass through.
String[] SelectNames(){
GeneType[] carry = geneNames[index];
String[] toReturn = new String[carry.length];
for(int i=0; i<toReturn.length; i++)
toReturn[i]=carry[i].name;
return toReturn;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论