0

I am trying to create an ArrayList of instances from another class in my program. In my case the class I am trying to create an ArrayList of is called record with the parameters firstname, lastname, and email. I have created another class called emailList and I am trying to instantiate the array list there. I am getting the error "The non-generic type 'ArrayList' cannot be used with type arguments".

Here is the code I have to create record objects in the Record class:

public class Record
    {
        private String firstname;
        private String lastname;
        private String email;
        public Record(String firstNameIn, String lastNameIn, String emailIn)
        {
            firstname = firstNameIn;
            lastname = lastNameIn;
            email = emailIn;
        }

In the emailList class I am trying to create an ArrayList in the emailList class using:

ArrayList<Record> eList = new ArrayList<Record>;

5
  • Was there a problem with your attempt to create an ArrayList? If so, what was it? Commented Jul 13, 2021 at 12:51
  • ArrayList is a very old class. It's available since .NET framework 1.x where we didn't have generics yet. Commented Jul 13, 2021 at 12:52
  • Im sorry I am pretty new to coding so bear with me here, but i want the list to contain records in an array not just a regular list. Is there not a difference in the format of the two? Commented Jul 13, 2021 at 12:54
  • 1
    There is not generic ArrayList in .NET, so instead of ArrayList<Record> you probably want List<Record>. Stay away from the old ArrayList. Commented Jul 13, 2021 at 12:56
  • @RyanCarney if you want to use an array, you can do with with var myArray = new Record[3]; myArray[0] = new Record("first", "last", "e-mail"); List<T> uses an array to store items which is expanded when the list reaches its capacity, e.g.: var myList = new List<Record>(); myList.Add(new Record("first", "last", "e-mail")); Commented Jul 13, 2021 at 12:59

2 Answers 2

2

ArrayList is not generic meaning you cannot provide an generic argument (e.g.: ArrayList<Record>), you need to instantiate it as var eList = new ArrayList();

For type-safe collection check List<T> under System.Collections.Generic namespace.

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

Comments

1

You don't need a type specifier for ArrayList:

ArrayList eList = new ArrayList();
eList.Add(new Record("Test", "Test", "[email protected]"));

2 Comments

even if i am trying to create an array list of objects from a class I created?
Nope. Hence new Record("Test", "Test", "[email protected]")

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.