选择类对象字段的数组。

huangapple go评论102阅读模式
英文:

select array of class objects field

问题

我有一个枚举和一个带有两个字段的类:

  1. enum types {
  2. nan,
  3. num,
  4. text,
  5. col,
  6. dress
  7. };
  8. class GeneType {
  9. String name;
  10. types type;
  11. GeneType(String name, types type) {
  12. this.name = name;
  13. this.type = type;
  14. }
  15. }

我还有一个使用这个类的对象数组:

  1. final GeneType[][] geneNames = new GeneType[][]{
  2. {
  3. new GeneType("Unknown", types.nan),
  4. new GeneType("Age", types.num),
  5. new GeneType("Fav number", types.num),
  6. new GeneType("Height", types.num),
  7. new GeneType("Name", types.text)
  8. },
  9. {
  10. new GeneType("Unknown", types.nan),
  11. new GeneType("Not Sure", types.nan),
  12. new GeneType("This", types.nan),
  13. new GeneType("That", types.nan),
  14. new GeneType("The other", types.nan),
  15. new GeneType("Eye color", types.col),
  16. new GeneType("Fav color", types.col),
  17. new GeneType("Name", types.text)
  18. }
  19. }

我想要使用最简单的方法来选择一组名称数组以设置选项卡名称(String[]):

  1. new Tabs(x, y, wid, hei, orientation, thingsToShow,
  2. geneNames[howMuchDataNeed(thingsToShow.size())].name)

其中,thingsToShow 是另一个类的 ArrayList。

如何选择 geneNames[index].name 数组?

英文:

I have an enum and a class with two fields:

  1. enum types {
  2. nan,
  3. num,
  4. text,
  5. col,
  6. dress
  7. };
  8. class GeneType {
  9. String name;
  10. types type;
  11. GeneType(String name, types type) {
  12. this.name = name;
  13. this.type = type;
  14. }
  15. }

I have also array of objects that are using this class

  1. final GeneType[][] geneNames = new GeneType[][]{
  2. {
  3. new GeneType("Unknown", types.nan),
  4. new GeneType("Age", types.num),
  5. new GeneType("Fav number", types.num),
  6. new GeneType("Height", types.num),
  7. new GeneType("Name", types.text)
  8. },
  9. {
  10. new GeneType("Unknown", types.nan),
  11. new GeneType("Not Sure", types.nan),
  12. new GeneType("This", types.nan),
  13. new GeneType("That", types.nan),
  14. new GeneType("The other", types.nan),
  15. new GeneType("Eye color", types.col),
  16. new GeneType("Fav color", types.col),
  17. new GeneType("Name", types.text)
  18. }
  19. }

I wand to select array of names using the simplest method to set tab names (String[])

  1. new Tabs(x, y, wid, hei, orientation, thingsToShow,
  2. 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],您将获得以下数组作为结果。

  1. [
  2. new GeneType("Unknown", types.nan),
  3. new GeneType("Age", types.num),
  4. new GeneType("Fav number", types.num),
  5. new GeneType("Height", types.num),
  6. new GeneType("Name", types.text)
  7. ]

然后,您需要将这个GeneType对象数组映射到它们对应名称的数组。为此,您可以使用Java的stream API,如下所示。

  1. String[] names = Arrays.stream(geneNames[index])
  2. .map(gene -> gene.name)
  3. .toArray(String[]::new);

更新: 不使用Java stream API实现

  1. GeneType[] selected = geneNames[index];
  2. String[] names = new String[selected.length];
  3. for (int i = 0; i < selected.length; i++) {
  4. names[i] = selected[i].name;
  5. }
英文:

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.

  1. [
  2. new GeneType(&quot;Unknown&quot;, types.nan),
  3. new GeneType(&quot;Age&quot;, types.num),
  4. new GeneType(&quot;Fav number&quot;, types.num),
  5. new GeneType(&quot;Height&quot;, types.num),
  6. new GeneType(&quot;Name&quot;, types.text)
  7. ]

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.

  1. String[] names = Arrays.stream(geneNames[index])
  2. .map(gene -&gt; gene.name)
  3. .toArray(String[]::new);

Update: Implementation without using Java stream APIs

  1. GeneType[] selected = geneNames[index];
  2. String[] names = new String[selected.length];
  3. for (int i = 0; i &lt; selected.length; i++) {
  4. names[i] = selected[i].name;
  5. }

答案2

得分: 1

尝试一下。

  1. int index = 1;
  2. String[] names = Arrays.stream(geneNames[index])
  3. .map(gene -> gene.name)
  4. .toArray(String[]::new);
  5. System.out.println(Arrays.toString(names));

输出:

  1. [Unknown, Not Sure, This, That, The other, Eye color, Fav color, Name]
英文:

Try this.

  1. int index = 1;
  2. String[] names = Arrays.stream(geneNames[index])
  3. .map(gene -&gt; gene.name)
  4. .toArray(String[]::new);
  5. System.out.println(Arrays.toString(names));

output:

  1. [Unknown, Not Sure, This, That, The other, Eye color, Fav color, Name]

答案3

得分: 0

以下是翻译好的部分:

  1. 可以通过在GeneType类中重写toString()方法来实现这一点
  2. 我希望这段代码能对你有所帮助
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. enum types {
  6. nan,
  7. num,
  8. text,
  9. col,
  10. dress
  11. };
  12. class GeneType {
  13. String name;
  14. types type;
  15. GeneType(String name, types type) {
  16. this.name = name;
  17. this.type = type;
  18. }
  19. @Override
  20. public String toString() {
  21. return name;
  22. }
  23. }
  24. class question {
  25. public static void main(String[] args) {
  26. final GeneType[][] geneNames = new GeneType[][]{
  27. {
  28. new GeneType("Unknown", types.nan),
  29. new GeneType("Age", types.num),
  30. new GeneType("Fav number", types.num),
  31. new GeneType("Height", types.num),
  32. new GeneType("Name", types.text)
  33. },
  34. {
  35. new GeneType("Unknown", types.nan),
  36. new GeneType("Not Sure", types.nan),
  37. new GeneType("This", types.nan),
  38. new GeneType("That", types.nan),
  39. new GeneType("The other", types.nan),
  40. new GeneType("Eye color", types.col),
  41. new GeneType("Fav color", types.col),
  42. new GeneType("Name", types.text)
  43. }
  44. };
  45. ArrayList<GeneType> names = new ArrayList<>();
  46. for (int i = 0; i < geneNames[0].length; i++) {
  47. names.add(geneNames[0][i]);
  48. }
  49. System.out.println(names);
  50. }
  51. }
英文:

You can do this using toString() overriding method in GeneType class.
I hope this code will help you.

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. enum types {
  4. nan,
  5. num,
  6. text,
  7. col,
  8. dress
  9. };
  10. class GeneType {
  11. String name;
  12. types type;
  13. GeneType(String name, types type) {
  14. this.name = name;
  15. this.type = type;
  16. }
  17. @Override
  18. public String toString() {
  19. return name;
  20. }
  21. }
  22. class question {
  23. public static void main(String[] args) {
  24. final GeneType[][] geneNames = new GeneType[][]{
  25. {
  26. new GeneType(&quot;Unknown&quot;, types.nan),
  27. new GeneType(&quot;Age&quot;, types.num),
  28. new GeneType(&quot;Fav number&quot;, types.num),
  29. new GeneType(&quot;Height&quot;, types.num),
  30. new GeneType(&quot;Name&quot;, types.text)
  31. },
  32. {
  33. new GeneType(&quot;Unknown&quot;, types.nan),
  34. new GeneType(&quot;Not Sure&quot;, types.nan),
  35. new GeneType(&quot;This&quot;, types.nan),
  36. new GeneType(&quot;That&quot;, types.nan),
  37. new GeneType(&quot;The other&quot;, types.nan),
  38. new GeneType(&quot;Eye color&quot;, types.col),
  39. new GeneType(&quot;Fav color&quot;, types.col),
  40. new GeneType(&quot;Name&quot;, types.text)
  41. }
  42. };
  43. ArrayList&lt;GeneType&gt; names = new ArrayList&lt;&gt;();
  44. for (int i = 0; i &lt; geneNames[0].length; i++) {
  45. names.add(geneNames[0][i]);
  46. }
  47. System.out.println(names);
  48. }
  49. }

答案4

得分: 0

这可能不是最佳选项,但它能够通过。

  1. String[] SelectNames(){
  2. GeneType[] carry = geneNames[index];
  3. String[] toReturn = new String[carry.length];
  4. for(int i=0; i<toReturn.length; i++)
  5. toReturn[i]=carry[i].name;
  6. return toReturn;
  7. }
英文:

This isn't propably the best option, but it pass through.

  1. String[] SelectNames(){
  2. GeneType[] carry = geneNames[index];
  3. String[] toReturn = new String[carry.length];
  4. for(int i=0; i&lt;toReturn.length; i++)
  5. toReturn[i]=carry[i].name;
  6. return toReturn;
  7. }

huangapple
  • 本文由 发表于 2020年7月23日 19:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63052746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定