Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Im a beginner in Java, I have the following code:

public class operations {
    public ArrayList<Integer> stream;
    public ArrayList<Integer> funct;
    public ArrayList<String> name;
    public ArrayList<Integer> funcgroup;
    public ArrayList<Integer> source;

I read and understood that classes in Java are comparable to structs in C. In my program, I have to get an input from the user for name and search through the class and provide the other fields (stream, func, funcgroup, source) as output. My question is similar to Efficient way to shuffle objects

From the first and best answer, I understand that rather than parallel lists, I should create a single list and then call a list with this class. But how do I add all my elements to the various fields in List in that case? I already have a separate ArrayList of these fields collected in another class.

share|improve this question

closed as off topic by Michael K, FrustratedWithFormsDesigner, Robert Harvey, gnat, Dynamic Jun 11 '13 at 16:53

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Is there only one stream,func,... for each name? –  kevin cline Jun 11 '13 at 15:35
possible duplicate of Efficient way to shuffle objects –  Michael K Jun 11 '13 at 16:02
@kevincline Yes there is only stream,func...for each name –  user93665 Jun 11 '13 at 16:03
If you understood the answer to your last question, why did you wind up with five lists again here? –  Robert Harvey Jun 11 '13 at 16:04
@MichaelK yes I read that post too...My question is 'how do I add the arraylist values to the single list' if I consider to go by the option 1 in that post?? –  user93665 Jun 11 '13 at 16:05
show 1 more comment

3 Answers

The first thing you need to do is organize your data into a more structure form. Something like this might work for you (you'll probably want to come up with a better name than "Thing", but your original question didn't give me much else to work with) :

public class Thing
  private Integer stream;
  private Integer funct;
  private Integer funcgroup;
  private Integer source;

  public Integer getStream()
    return stream;
  public Integer setStream(Integer i)
    stream = i;

  public Integer getFunct()
    return funct;
  public Integer setFunct(Integer i)
    funct = i;

  public Integer getFuncGroup()
    return funct;
  public Integer setFuncGroup(Integer i)
    FuncGroup = i;

  public Integer getSource()
    return Source;
  public Integer setSource(Integer i)
    Source= i;

Now your Operations class just needs an hashmap to store the associations between names and "things":

public class Operations{
    public HashMap<String, Thing> things = new ArrayList<String, Thing>();

  public getThing(String name)
     return things.get(name);

  public addThing(String name, Thing thing)

The Thing class will store the structure you've shown (if I understand it correctly). The Operations class has a HashMap of Things - this allows you to index by a String (your "name" field) and then find them by the same value.

Then, to get a random ordering of the things in you Operations class you could add a method to Operations like this:

  public List<Thing> getShuffleThings()
     Collections.shuffle( new ArrayList<String>(things.values()));

So the final example might look something like this:

Thing thing1 = new Thing();
thing1.setFunct(new Integer(123));
thing1.setStream(new Integer(456));
Thing thing2 = new Thing();
thing2.setSource(new Integer(789));

Operations ops = new Operations();
ops.addThing("thing2", thing2);
List<Thing> shuffled = ops.getShuffledThings();

What's not clear to me is if name is only needed for indexing the objects, or if you need it to be a part of Thing.

(I haven't actually compiled this so there may be minor bugs)

share|improve this answer
Thanks for the explanation :) –  user93665 Jun 11 '13 at 16:27
add comment
public class Operation {
    public Integer stream;
    public Integer funct;
    public String name;
    public Integer funcgroup;
    public Integer source;

List<Operation> operations = new List<Operation>();

Operation operation = new Operation();
operation.stream = 1; // values are merely illustrative
operation.funct = 2;
operation.name = "Fred";
operation.funcgroup = 4;
operation.source = 5;

share|improve this answer
Obviously this is greatly stripped down for simplification purposes. Eventually, if you create a public API for your Operation class, you might want getter and setter methods, and maybe even an Interface. Java can be quite wordy that way. –  Robert Harvey Jun 11 '13 at 16:15
That helps to some extent. What if I already have the values for stream,func,name,funcgroup.. already stored in an array? How do I add it to the List then? That is I have an array each for func,name,..etc –  user93665 Jun 11 '13 at 16:18
The last line of code adds it to the list. If you want to translate it from your existing arrays, you need to write some code to loop through the arrays, create your objects, and add them to the list. –  Robert Harvey Jun 11 '13 at 16:18
Can I do operation.stream = stream[]; ?? –  user93665 Jun 11 '13 at 16:19
More like operation.stream = stream[i], where i is your for loop variable. –  Robert Harvey Jun 11 '13 at 16:28
show 2 more comments

Java classes can be thought of as structs in C in that they can be containers for holding data and nothing more. However, unlike structs in C, they cannot be instantiated like aggregate classes, which is to say like this:

struct C
  int a;
  double b;
C c = {1, 2.0};

For that, you will need to create a new instance of a class and set its data. The typical approach for this is to add private members and create public getters and setters (despite making this approach far more verbose than C++).

So applied to the C++ example above, the Java version would be:

public class C {
  private int a;
  private double b;

  public C(int a, double b) {

  public int getA() {
    return a;
  public void setA(int a) {
    this.a = a;
  public double getB() {
    return b;
  public void setB(double b) {
    this.b = b;

C c = new C(1, 2.0);

Despite being a little more verbose, this is generally considered good practice in Java, since it allows you full control over members of your class if you ever decide to change the implementation later without rewriting every line in your code whenever you access members a or b.

Applied to a List, you can simply instantiate the List and for each item you wish to add to that List, instantiate the object and add it to the list, so:

List<C> list = new ArrayList<C>();
for(int i=0; i<500; i++) {
  C c = new C(i, i*2.0);

In this way, you have the advantage that you can look up an instance of C by index and have all the information there rather than have to look up in multiple arrays. The only problem which remains at this point is how you would find an object by one of its properties. The only way to do this with a List is to search for it:

C foundC = null;
for(C c : list) {
  if(c.getA() == 20) {
    foundC = c;

I'll leave it as an exercise to you how to apply this to your specific case. If you're interested in knowing more, then I would recommend you find out how Maps work. I'll leave that for you to discover on your own. You can think of maps as Lists accessible by keys rather than indices.

share|improve this answer
And again with all the Getters and Setters. So much ceremony for so little benefit. Is this really the way things work in Java? –  Robert Harvey Jun 11 '13 at 16:09
It depends. Did you want to follow best practice or not? I explain in my answer why you should do it this way. –  Neil Jun 11 '13 at 16:15
He's a beginner. Crawl before you walk. –  Robert Harvey Jun 11 '13 at 16:16
And in your opinion, getters and setters are beyond his ability to understand? This is crawling. I'd assume teach him the proper way to crawl. –  Neil Jun 11 '13 at 16:20
I can see how a seasoned programmers instinct is to show everything "the right way." I just don't agree with that sort of "kitchen sink" teaching approach, that's all. By the way, you missed something; best practices require an Interface and TDD unit tests first. –  Robert Harvey Jun 11 '13 at 16:24
show 4 more comments

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