0

I have a java class which has a List as following:

public class TestEntity {
   private String name;
   private List<DataEntry> dataEntries;
}

class DataEntry {
    private String para1;
    private String para2;
    private String para3;
}

How could i add a DataEntry's instance into a TestEntity's instance?

1
  • 5
    by writing code that does so. there are several ways, but you should at least try something Commented Dec 21, 2017 at 9:40

3 Answers 3

2

First you need to ensure that your list is actually not null, like:

private final List<DataEntry> entries = new ArrayList<>();

and then you can do something like

entries.add(new DataEntry());

or probably more useful:

DataEntry entry = new DataEntry();
entry.para1 = ...

entries.add(entry);

Of course, the more realistic thing would be to add a custom constructor to the DataEntry class, so that you can pass the required arguments via the constructor.

Or, you add a method

void addEntry(DataEntry entry) {
  entries.add(entry);

to your TestEntity class.

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

3 Comments

It's an annoying feature of this site. These kind of injustices make my blood boil. My upvote can't remove the down, but at least it erases the negative display.
@duffymo Yeah well. I suspect there could be a "great" duplicate somewhere, but after 1 minute of searching I gave up and decided that I would be able to write an exact answer more quickly than finding that "perfect duplicate". Thanks for your encouraging words.
There are duplicates for almost everything. At this game, no more new questions or almost.
1

Pretty simple if you just instantiate it.

dataEntries.add(new DataEntry());

Comments

1

There are two possiblities:

First: You add a constructor to add an entry:

public class TestEntity {
   private String name;
   private List<DataEntry> dataEntries;
   public TestEntity (String name,DataEntry entry){
     this.name = Name;
     this.dataEntry = new ArrayList<>();
     this.dataEntry.add(entry)
   }
}

Second: You add an addDataEntry function:

public class TestEntity {
   private String name;
   private List<DataEntry> dataEntries;
   public void addDataEntry(DataEntry entry){
     if (this.dataEntry == null){
         this.dataEntry = new ArrayList<>();
     }
     this.dataEntry.add(entry)
   }
}

7 Comments

@GhostCat About downvotes i do not longer think about at SO. But i do not think that my answer is worse. Why we should not have a construtor to initialize a list with a single entry? I do not write that must be the only constructor.
Well. It just feels strange on a conceptual level. But whatever, its xmas time ;-)
@GhostCat I do not see any strange thing there. initialize a list with one value and add more values later.
And to me, that sounds like indication of something going in the wrong direction. If you know your elements at creation time, provide them there. If you know them later on, add all of them later on. Having a constructor taking a single list element and having a method to add more elements later ... to me that sounds like "non-segregation" of concerns.
Yeah, I'm with GhostCat on this one. A constructor that puts a single argument into a List where you can later add other elements just feels wrong.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.