- Home /
Print arraylist values to console
With the help of info here I created a random unique number generator in an arraylist.
I want to print the arraylist content to my console but I only get this message: System.Collections.ArrayList
If I use .Count or .IndexOf it works. I can't find a solution anywhere on the internet. And Unity doesn't support arraylist in their documentation at all.
Thanks!
My code:
var totalHumans:int = 8;
var aHuman = ArrayList();
function Start ()
{
while (aHuman.Count < totalHumans)
{
var human = Random.Range(0, 17);
if (!aHuman.Contains(human))
aHuman.Add(human);
}
Debug.Log(aHuman);
}
Answer by Molix · Jun 08, 2011 at 01:48 PM
You can iterate over the list with something like:
for( var human in aHuman )
{
Debug.Log( human );
}
Answer by jana-jarecki · Jan 17, 2017 at 01:29 PM
In case anyone is still following up, here is what I use to get a one-line array output
Debug.Log("Human = " +String.Join("",
new List<int>(aHuman)
.ConvertAll(i => i.ToString())
.ToArray()));
This is a very nice implementation, you can also concat several strings into one in the ConvertAll section. I use it to log child-parent name pairs.
For efficiency , ToArray() is not needed because unity already treat List<> similar to the way they treat array , so without ToArray() it still work and give better performance .
Answer by Pflegeleichtt · Jun 08, 2011 at 01:48 PM
I think your're looking for an Array().
No, because I have to use .Contains. Which is only supported by ArrayList();
Answer by iggy · Jun 08, 2011 at 01:51 PM
here are all the methods for ArrayList.
http://msdn.microsoft.com/en-us/library/system.collections.arraylist_methods(v=VS.85).aspx
i don't think there is one you seek.
why don't you add in your WHILE loop something like this:
Debug.Log("human"+aHuman.Count+"="+human);
Yes I read a lot on the pages there. Here the example staits: "The following code example shows how to create and initialize an ArrayList and how to print out its values". http://msdn.microsoft.com/en-us/library/7x4b0a97%28v=VS.80%29.aspx It's JavaScript, so I suppose it's possible? Bit difficult for me to understand.
Anyway thanks, I'll try what you said :)
yea just be careful when copy-pasting from $$anonymous$$SDN.
There is no "Console.Write" in Unity but use something similar like "Debug.Log()", etc...
Your answer
Follow this Question
Related Questions
Storing Level Data in Array of Array (grid-based game) 1 Answer
Assigning value stored in an array with get{} using cSharp 1 Answer
How to display data from array,arraylist while clicking the button? 1 Answer
Collections don't work! 1 Answer
How to let multiple values contribute two one value constantly 1 Answer