0
 ArrayList<Integer> initgenome = new ArrayList<>(5000); initgenome.addAll(Collections.nCopies(10, 1)); initgenome.addAll(Collections.nCopies(4990, 0)); ArrayList<Integer> genometwo = new ArrayList<>(5000); genometwo.addAll(Collections.nCopies(10, 2)); genometwo.addAll(Collections.nCopies(4990, 0)); ArrayList<Integer> genomethree = new ArrayList<>(5000); genomethree.addAll(Collections.nCopies(10, 3)); genomethree.addAll(Collections.nCopies(4990, 0)); ArrayList<Integer> genomefour = new ArrayList<>(5000); genomefour.addAll(Collections.nCopies(10, 4)); genomefour.addAll(Collections.nCopies(4990, 0)); 

I am doing a project in Eclipse using Java and I have my objects randomly picking one of these genomes when initializing. I was curious if there was anyway to set a color to a each arraylist so you could visually see the genome that got picked? Thanks!

4
  • 1
    How do you expect to see the color? On the screen? Commented Dec 12, 2016 at 20:14
  • When the program is run, there is a grid that pops up with my microbes on the grid. Right now, they are all blue. Commented Dec 12, 2016 at 20:39
  • What GUI toolkit do you use? Swing, JavaFX? Commented Dec 12, 2016 at 22:15
  • I believe I am using AWT Commented Dec 13, 2016 at 21:07

1 Answer 1

3

Add a mapping (Map):

Map<ArrayList<Integer>, String> genomesToColors = new HashMap<>(); genomesToColors.add(initgenome, "red"); genomesToColors.add(genometwo, "blue"); genomesToColors.add(genomethree, "green"); genomesToColors.add(genomefour, "purple"); 

Then after you have picked your ArrayList:

ArrayList<Integer> genome = // however you do it. String color = genomesToColors.get(genome); 

Since your idea of a genome is getting more complex, you may want to create a Genome class. You can give it a sequence attribute and a color attribute.

public class Genome { public List<Integer> sequence = new ArrayList<Integer>(); public String color; // Getters, setters, whatever you want. } 

EDIT: Since your genome may mutate, I would suggest going with the class based solution. It is more easily extensible, and takes advantage of OOP.

Sign up to request clarification or add additional context in comments.

3 Comments

Having mutable object as a map key is generally a bad idea.
True, if the genome is being modified (mutated?) in anyway, the class based solution is probably better. If the genomes are not mutating, an immutable list should be used. stackoverflow.com/questions/9973596/arraylist-as-key-in-hashmap
The genome is able to mutate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.