Retrieving objects from ArrayList - cast to type?

You can use linq to do this easily:

This will cast all items to string and return an IEnumerable<string>. It will fail if any items can't be cast to a string:

items.Cast<string>();

This will cast all items that can be to a string and skip over any that can't:

items.OfType<string>();

You can access single elements in the ArrayList doing a cast, for example...

string s = myArrayList[100] as string;
myArrayList.Remove("hello");
myArrayList[100] = "ciao"; // you don't need a cast here.

You can also iterate through all elements without a cast...

foreach (string s in myArrayList)
    Console.WriteLine(s);

You can also use CopyTo method to copy all items in a string array...

string[] strings = new string[myArrayList.Count];
myArrayList.CopyTo(strings);

You can create another List<string> with all items in the ArrayList. Since ArrayList implements IEnumerable you can call the List<string> constructor.

List<string> mylist = new List<string>(myArrayList);

But this doesn't makes much sense... why you don't just use List<string> directly? Using directly a List<string> seems more useful to me, and is faster. ArrayList still exists mostly for compatibility purposes since generics were introduced in version 2 of the language.

I just noticed that there may be an error in your code:

    while (line != null)
    {   
        fileContent.Add (line);
        line = reader.ReadLine ();
    }

Should be instead

    for (;;)
    {   
        string line = reader.ReadLine();
        if (line == null)
            break;
        fileContent.Add(line);
    }

You cast each element on its own before using it.


Comments

  1. Nova

    • 2016/7/5

    You can use linq to do this easily: This will cast all items to string and return an IEnumerable<string> . It will fail if any items can't 

  2. Princeton

    • 2017/9/14

    The ArrayList items are supposed to store the object items, although I'm not quite sure why I need two arrays? The problem I have is: when you retrieve the "items" from the array, they have to be cast to a type, string , (if I understand it correctly, otherwise they are returned as objects ?).

  3. Shkreli

    • 2020/6/18

    We covered primitive conversions in this article, and we'll focus on references casting here to get a good understanding of how Java handles 

  4. Brian

    • 2021/3/31

    Solution 1. You declare temp_result as of type object. final_result.Add () accepts parameters of type System.Collections.Arraylist. The latter cannot implicitly be converted to the former. If you know that display_messages () does return an ArrayList, then you should declare temp_result as such in the first place.

  5. Lucca

    • 2019/8/30

    An ArrayList is an object that can store a group of other objects for us and get(), you must put in a cast to indicate the type of pointer you expect.

  6. Landyn

    • 2015/10/18

    An arraylist stores objects so in order to retrieve them you need to cast them to the appropiate type. Ex. ArrayList myarraylist=new ArrayList(); myarraylist.Add("hello"); In order to retrieve the string I need to cast it to string. string mystring=(string)myarraylist[0]; Regards

  7. Valentino

    • 2015/8/17

    "Unable to cast object of type 'System.Object[]' to type 'System.Collections.ArrayList'." Now tell me how should i retrieve value from an 

  8. Parker

    • 2020/10/1

    You can use casting to explicitly name the type of the object in teh arraylist Dim al As New ArrayList. For x As Int32 = 1 To 5. Dim c As New Cust. Dim sCustId As String = "CUST" & x. c.custid = sCustId. c.timestamp = Now. al.Add (c) Next.

  9. Romeo

    • 2016/4/22

    Then we are creating an object array obj1 and using ToArray method with mylist we will assign the Arraylist elements to the object array.

  10. Rohan

    • 2018/9/14

    now at the point when i try to type cast ArrayList element to myClass in vb, it gives me below exception,-----Unable to cast object of type Library.myClass to type myClass When casting from a number, the value must be a number less than infinity Make sure the source type is convertible to the destination type-----

  11. Tommy

    • 2020/8/2

    We have to make a corresponding change where the ArrayList object is created a cast to be used when objects are retrieved from it, since the type of its 

  12. Matthew

    • 2021/5/15

    I THINK so, but I'd better defer to others. I suppose that if the Gun object had a property called myType and you set it, upon creation, to either "Gun" or "MachineGun", then you could get every element in the ArrayList, casting it as a (Gun) and then check the myType value, going back to the ArrayList and re-casting the element if you found that it was actually a (MachineGun).

  13. Koa

    • 2021/7/22

    Using wild cards, you can convert ArrayList<String> to ArrayList<Object> as − ArrayList<String> stringList = (ArrayList<String>)(ArrayList< 

  14. Valentini

    • 2017/12/12

    All of the objects in the ArrayList object will be cast to the Type specified in the type parameter. The elements are copied using Array.Copy, which is an O(n) operation, where n is Count. See also. Type

  15. Bjorn

    • 2019/8/31

    You must cast this result to the data type that was used when loading the array. String str = (String)myList.get(3); // get the item at position 3 (fourth item 

  16. Turner

    • 2018/11/22

    The following example shows you how to get various type objects: var ArrayList = Java.type("java.util.ArrayList"); var intType = Java.type("int"); 

  17. Ruben

    • 2018/9/29

    The parameter type becomes the compiletime type of the element arguments in to retrieve the object from the collection and cast it to the expected type.

Comments are closed.

Recent Posts