0

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
  • You could make a method that returns the array based on the index.
    – Kayaman
    Jul 23, 2020 at 11:11

4 Answers 4

2

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
  • this could be the answer that I want to implement, but unfortunately I thougth, that my android processing compiler will pass that code and it would, but the debuger catched two errors (associated with -> and :: characters
    – Erdien
    Jul 23, 2020 at 22:35
  • Then you may be using an older JDK version (<JDK8) which does not support these stream APIs. So I have updated the answer with an alternate implementation that does not use stream APIs. Jul 24, 2020 at 3:06
1

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]
0

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);
    }
}
0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.