Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

ArrayList(s) in Java?

$
0
0
Posted Image

That's the HW question^

Here is the code I've done to try and complete the question:

ArrayList<String> name_list = new ArrayList<String>();
	name_list.add("Fred");
	name_list.add("Jane");
	name_list.add("Zoe");
	System.out.println(name_list);
	
	
	ArrayList<Integer> age_list = new ArrayList<Integer>();
	age_list.add(21);
	age_list.add(43);
	age_list.add(37);
	System.out.println(age_list);



The only problem I have here (I'm hoping I've done the above^ correct lol), is that I have to run the method:

private static void PrintDataArray(ArrayList<Data> array) 
	{
		for(int i=0;i<3;++i)
		{
			array.get(i).Print();
		}
	}



and add "Harry aged 78 between Jane and Zoe" when printing it out. The problem is I am unable to do that! Which makes me think I've done the first bit wrong. I always get an error when using of type Data;

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

//Error is
//The method add(Data) in the type ArrayList<Data> is not applicable for the arguments <String>/<Integer>




Am I making this more complicated than need be?

I don't know another way of approaching this. Any help please? Thank you!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>