1

I created an object N, which has some attributes, like this:

public class LogEvidence {
private String comment;
private String url;
private String time;

public LogEvidence(String comentario, String url, String tiempo) {
    super();
    this.comment = comentario;
    this.url = url;
    this.time = tiempo;
}

public String getComentario() {
    return comment;
}

public void setComentario(String comentario) {
    this.comment = comentario;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getTiempo() {
    return time;
}

public void setTiempo(String tiempo) {
    this.time = tiempo;
}

@Override
public String toString() {
    return "LogEvidence [comentario=" + comment + ", url=" + url + ", tiempo=" + time + "]";
}
}

Now I want to do something like this:

ArrayList<LogEvidence>log = new ArrayList<LogEvidence>();

I want go through the list and add all the attributes to my object, I mean something like this:

log.setComment("comment one");
log.setUrl("http://google.com");
log.setTime("04:20");

Maybe this is not possible and I have to do something like the following?

List list= new List();
 LogEvidence object1= new LogEvidence ();
  object1.setComment("comment");
  object1.setUrl("http://url.com");
  object1.setTime(20);
lista.add(object1);
3
  • You need to get an object from the list to call your accessor methods on it. log.get(0).setComment("blah blah"); returns the first element (index 0) from your list and calls setComment Commented Oct 4, 2016 at 21:51
  • Ideally the last code is the correct way Commented Oct 4, 2016 at 22:05
  • 1
    It's not clear what you want to do. What is log.setComment(...) meant to do? Commented Oct 4, 2016 at 22:06

2 Answers 2

1

This is how you can do it:

Create an object of LogEvidence.

LogEvidence logEvidence = new LogEvidence();
logEvidence.setComentario("comment one");
logEvidence.setUrl("http://google.com");
logEvidence.setTiempo("04:20");

and add it into the array list.

log.add(logEvidence);

So, you can then create more objects and keep putting in the list. Since your list is named as log, so that is why you will add in log

Explaining it a little more, it should be something like this:

List<LogEvidence> logEvidenceList = new ArrayList<>();

LogEvidence logEvidence1 = new LogEvidence();
logEvidence1.setComentario("comment one");
logEvidence1.setUrl("http://google.com");
logEvidence1.setTiempo("04:20");
logEvidenceList.add(logEvidence1);

LogEvidence logEvidence2 = new LogEvidence();
logEvidence2.setComentario("comment one");
logEvidence2.setUrl("http://google.com");
logEvidence2.setTiempo("04:20");
logEvidenceList.add(logEvidence2);

....
....
....

Or through constructor call, this will become more concise and readable.

List<LogEvidence> logEvidenceList = new ArrayList<>();

LogEvidence logEvidence1 = new LogEvidence("comment one","http://google.com","04:20");
logEvidenceList.add(logEvidence1);

LogEvidence logEvidence2 = new LogEvidence("comment one","http://google.com","04:20");
logEvidenceList.add(logEvidence2);

....
....
....

Now, when you want to retrieve objects from the list, you can traverse the list and get one by one like;

for (LogEvidence evidence : logEvidenceList) {
    System.out.println(evidence);
}

For more information about ArrayList

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

Comments

0

As Kon commented, you are trying to act upon an object that will be within the list, rather than the list itself.

There are a few ways to do this, but since you have a parameterized construtor, the easiest would be this:

ArrayList<LogEvidence> log = new ArrayList<LogEvidence>();
log.add(new LogEvidence("comment one", "http://url.com", "04:20");

What's being done:

We create the list with new ArrayList<LogEvidence>() and assign it to a variable called log.

Then, we create a new LogEvidence object, already assigning it's data through it's constructor's parameters, with new LogEvidence("comment one","http://url.com","04:20").

Since it will be stored in the list, we can use it anonymously. We add it to the list directly, using the list's .add(E e) method, rather than assigning it to a variable.


You don't need to define or assign unnecessary variables, in this case.

And if you need to access any specific LogEvidence from the list, you can use the .get(int index) method. In this case, log.get(0), to return the first element, would return the object you just inserted in the example code above.

You can also use a FOR-EACH LOOP to act upon all objects in the list, in iterating manner. Here is an example that prints the list:

for (LogEvidence evidence : log) {
    System.out.println(evidence);
}

Comments

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.