0

I am trying to add items of type data to an arraylist, which would display the name and age of 3 individuals in the form of ("Name", Age), I have the following code

import java.util.ArrayList;
public class Arraylist{
public static void main(String args[]){
    Data x = new Data("mark",41);
    x.Print();

    ArrayList<Data> arrl = new ArrayList<Data>();

        arrl.add("phil",21);
        arrl.add("sarah",43);
        arrl.add("william",37);

            for(int i=0;i<arrl.size();++i)
            {
                arrl.get(i).Print();
            }

I get an error on the words "add", saying that the method is not applicable for the argument. Any help is much appreciated.

2
  • 2
    Can you show where in the API documentation for ArrayList there is an add method that takes a String and then an integer number as its arguments? Commented Feb 24, 2014 at 20:51
  • Erm, I wouldn't know. maybe am going about this all wrong. Hence am here asking for a bit of guidance. Commented Feb 24, 2014 at 20:53

1 Answer 1

4

I think you want this:

arrl.add(new Data("phil",21));
arrl.add(new Data("sarah",43));
arrl.add(new Data("william",37));
Sign up to request clarification or add additional context in comments.

1 Comment

Only this is the data is printed all on a single line as opposed to one one each line.

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.