[java] How to add an object to an ArrayList in Java

I want to add an object to an ArrayList, but each time I add a new object to an ArrayList with 3 attributes: objt(name, address, contact), I get an error.

import java.util.ArrayList;
import java.util.Scanner;
public class mainClass {
    public static void main(String args[]){

        Scanner input = new Scanner(System.in);
        System.out.println("Plz enter Name : ");
        String name = input.nextLine();
        System.out.println("Plz enter Address : ");
        String address = input.nextLine();
        System.out.println("Plz enter ContactNo : ");
        String contact = input.nextLine();


        ArrayList<Data> Contacts = new ArrayList<Data>();
        Data objt = new Data();
        Contacts.add(objt.Data(name, address, contact));
    }
}

Here, Data is the class of which I'm trying to create an object and pass it to an ArrayList.

public class Data {

        private String name = "";
        private String address = "";
        private String cell = "";


        public void Data(String n, String a, String c){

            name = n;
            address = a;
            cell = c;
        }
        public void printData(){

            System.out.println("Name\tAddress\tContactNo");
            System.out.println(name + "\t" + address + "\t" + cell);
        }
}

This question is related to java arraylist add

The answer is


Contacts.add(objt.Data(name, address, contact));

This is not a perfect way to call a constructor. The constructor is called at the time of object creation automatically. If there is no constructor java class creates its own constructor.

The correct way is:

// object creation. 
Data object1 = new Data(name, address, contact);      

// adding Data object to ArrayList object Contacts.
Contacts.add(object1);                              

change Date to Object which is between parenthesis


You have to use new operator here to instantiate. For example:

Contacts.add(new Data(name, address, contact));

Try this one:

Data objt = new Data(name, address, contact);
Contacts.add(objt);

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to arraylist

Adding null values to arraylist How to iterate through an ArrayList of Objects of ArrayList of Objects? Dynamically adding elements to ArrayList in Groovy How to replace existing value of ArrayList element in Java How to remove the last element added into the List? How to append elements at the end of ArrayList in Java? Removing Duplicate Values from ArrayList How to declare an ArrayList with values? In Java, can you modify a List while iterating through it? Load arrayList data into JTable

Examples related to add

Add a new item to recyclerview programmatically? momentJS date string add 5 days How to add an object to an ArrayList in Java ListView with Add and Delete Buttons in each Row in android How to add new column to MYSQL table? How to add text to an existing div with jquery Datetime in C# add days How to add hours to current time in python How To Add An "a href" Link To A "div"? How to use ArrayList.addAll()?