0

This is the array code I have so far:

ArrayList<Data> arrl = new ArrayList<Data>();
    arrl.add("Tim", 23);    

Need to know how to an integer and a string to the array.

For Example:

names: and ages: Tim 23 Max 56 Clare 43

I know how to add integers OR strings to array-lists but i can't figure how to incorporate both in the same array.

6
  • 3
    Your ArrayList is meant to store Data objects. So add Data objects. Commented Jan 31, 2014 at 15:39
  • 1
    also, just for list interchangability (and a more flexible design) use the List interface on the assignment variable. Like: List<Data> arrl = new ArrayList<Data>(); Commented Jan 31, 2014 at 15:41
  • Assuming you have a data class, List<Data> arrl = new ArrayList<Data>(); then arrl.add(new Data("Tim", 23)). Depending on your usage (ie if you will know the names and want to look up the ages) you could use a Map instead. If this is relevant to you I can give an example. Commented Jan 31, 2014 at 15:44
  • the OP could also use List<Object> arrl = new ArrayList<Object>(); arrl.add("Tim"); arrl.add(new Integer(23)); -- depends what the OP is after I suppose. Commented Jan 31, 2014 at 15:48
  • @SnakeDoc While that is valid, but I can't think of a single instance where it would be beneficial to throw Strings and Integers into list together. Commented Jan 31, 2014 at 15:59

6 Answers 6

5

Your list is taking objects of type Data. So, create a Data class that contains a String for the name and int for the age. Create a Data object for each entry you want and add it to your arrl list.

public class Data {

    private String name;
    private int age;


    /**
     * Constructor
     */
    public Data(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getters and Setters go here.
}

In this example I used a constructor to allow easy construction of a Data object with a name and age.

List<Data> arrl = new ArrayList<Data>();
arrl.add(new Data("Tim", 23)); 
Sign up to request clarification or add additional context in comments.

4 Comments

please adjust your interface usage to best practice of List<Data> arrl = new ArrayList<Data>();
As long as you do that, use Java 7 diamond notation :)
@MadPhysicist that is the "diamond notation" lol, it's more type-safe than just using new ArrayList();
Diamond notation is this: List<Data> arrl = new ArrayList<>();. You let the compiler figure out the generic type. It's a small difference, and I'm just jumping on the anal wagon.
0

You could have a class Person for instance, where you save the name and age of a person.

Then, you have a ArrayList of Person, and you can store and access that information.

Comments

0

ArrayList is a class that use generics, it means type-safety, so when you declare a class that use generic in its class definition you have to declare the type when making the instance.

If you want to store Stringobjects declare ArrayList in this way:

ArrayList<String> saveString = new ArrayList<String>

You can make a class Person with those instance variables and resolve your problem. For example:

public class Person {

private String name;
private int age;

public Person(String name,int age){
  this.name = name;
  this.age = age;
}

}

And then you can use in this way:

ArrayList<Person> people = new ArrayList<Person>();
people.add(new Person("Luis Alberto",15));

Comments

0

Your arraylist is made for Data objects that means you have to create Data object to put it there, `this is what i mean by that arrl.add(new Data("Tim",44));
Make sure that you have class Data with constructor for string and int

Comments

0

Something like this:

public class Main {

    public static void main(String[] args) {
        ArrayList<Person> arrl = new ArrayList<>();
        arrl.add(new Person("Tim", 23));
    }
}

class Person {
    private String name;
    private int age;

    Person (String name, int age) {
        this.name = name;
        this.age = age;
    }
}

Comments

0

I think the best approach is what is proposed by Wabs and Ashot Karakhanyan. But if your need, is to have the name and age as different items of your list, you can use the following:

List<Object> arrl = new ArrayList<Object>();
arrl.add("name");
arrl.add(23);

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.