0

What is the equivalent way to create a list of objects in Java like the C# way of doing it below (example from the web)? I have to write something in Java, but its new to me. I can't seem to find what I'm looking for when searching the web either.

List<Author> AuthorList = new List<Author>();

/*The following code snippet creates the Author objects and adds them to the List. */

AuthorList.Add(new Author("Mahesh Chand", 35, "A Prorammer's Guide to     ADO.NET", true, new DateTime(2003,7,10)));

AuthorList.Add(new Author("Neel Beniwal", 18, "Graphics Development with  C#", false, new DateTime(2010, 2, 22)));

AuthorList.Add(new Author("Praveen Kumar", 28, "Mastering WCF", true, new DateTime(2012, 01, 01)));

AuthorList.Add(new Author("Mahesh Chand", 35, "Graphics Programming with GDI+", true, new DateTime(2008, 01, 20)));

AuthorList.Add(new Author("Raj Kumar", 30, "Building Creative Systems", false, new DateTime(2011, 6, 3)));
2
  • 3
    You're close, but remember in java, method names start with lowercase instead of uppercase (and your variables should be too!). Try .add(.. instead of .Add( Commented Jun 30, 2016 at 15:32
  • 3
    In java List is an interface so you can't instatiate it. Try List<Author> AuthorList = new ArrayList<Author>(); Commented Jun 30, 2016 at 15:33

4 Answers 4

2

In Java, you would just use a generic ArrayList.

import java.util.*;    
...    
ArrayList<Author> AuthorList = new ArrayList<Author>();
AuthorList.add(new Author("Mahesh Chand", 35, "A Prorammer's Guide to     ADO.NET", true, java.time.LocalDateTime.of(2003,7,10)));
...

Note that you could type the variable as 'List':

List<Author> AuthorList = new ArrayList<Author>();

But the original doesn't type to an interface, so the exact Java equivalent perhaps shouldn't either.

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

5 Comments

That was probably a great idea including the import statement, I can see how that would have probably been missed too.
@Bobby: I'm not sure what you mean by 'getting an option'.
When I try your suggestion I'm getting an error that my constructor cannot be applied to given types. "required: no arguments". There must be an issue with my class in how its setup? I have private variables with public getters and setters.
I was obviously getting myself confused with the basics in Java! It was my constructor that I needed to fix. Thanks for the help.
@DaveDoknjas: Just as in what options are accepted or showing up in the intellisense after I type the ".add( " to my instantiated object.
2

List is an interface. use ArrayList as follows:

List<Author> AuthorList = new ArrayList<Author>();

Comments

1

List is an interface in java, so you cannot new a List. You can use an ArrayList, like this:
List<Author> AuthorList = new ArrayList<Author>();

Comments

0

In Java, List is an interface. Hence, you cannot instantiate it. You have to find the implementation that fits your need. Generally, ArrayList is a good starting point. LinkedList is quite useful too.

So you can create your list like this :

List<Author> list = new ArrayList<>();

(Note that the variable name begins with a lowercase letter : it is a Java convention).

Then, you can add some elements. In Java, every methods names shall start with a lowercase letter too. On a list, you can use the method add().

list.add(new Author(...));

Note : when you want to create a list of predefined values, you can use this shortcut method :

List<Author> authors = Arrays.asList(
    new Author(...),
    new Author(...)
);

But the resulting list is immutable. Trying to add an element will raise an exception at runtime.

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.