Q: Array question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Arvidsson

    Q: Array question.

    Hi all you gurus!

    Whats the biggest difference between using a string[] x; or an ArrayList x;

    When to use wich?

    Regards
    Martin


  • Adam Clauss

    #2
    Re: Array question.

    When you have the explicit array (string[]), anytime you pull an item out of
    the array, ex:
    string s = x[0];

    you (and the compiler) can safely assume that x[0] is actually a string.
    ArrayList on the other hand holds "System.Obj ect" - basically ANY type.
    Thus, to retrieve an item, you would need to cast it. There are a couple
    ways to do this:
    string s = (string)x[0];
    string s = x[0] as string;

    The first will throw an InvalidCastExce ption in the event that x[0] is NOT a
    string, so use it only if you are 100% completely positive the list only
    holds strings. The second will simply return a 'null' instead of throwing
    an exception.

    Also - a string[] is a fixed size array. Once the array is created, you
    cannot add new items, or remove items. With ArrayList, it dynamically
    adjusts the size, so you can add/remove items as you wish.

    In .NET 2.0, with the introduction of generics, you can use List<string(or
    List<intor List<SomeObj-- whatever you need). This is the equivalent of
    ArrayList (in terms of being able to add/remove items) with the added
    advantage of not needing to cast items out of it.

    --
    Adam Clauss

    "Martin Arvidsson" <yeahright@ms.c omwrote in message
    news:O14trus$GH A.5060@TK2MSFTN GP02.phx.gbl...
    Hi all you gurus!
    >
    Whats the biggest difference between using a string[] x; or an ArrayList
    x;
    >
    When to use wich?
    >
    Regards
    Martin
    >

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Array question.

      Martin,

      An array is immutable, it means that at every (size) change a new one will
      be created and the old one be set ready for the GC. If this is about strings
      it is even more, because strings themselfes are as well immutable.

      In an arraylist will only a reference to new object be added to or an old
      one removed from the list.

      Cor

      "Martin Arvidsson" <yeahright@ms.c omschreef in bericht
      news:O14trus$GH A.5060@TK2MSFTN GP02.phx.gbl...
      Hi all you gurus!
      >
      Whats the biggest difference between using a string[] x; or an ArrayList
      x;
      >
      When to use wich?
      >
      Regards
      Martin
      >

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Array question.

        Cor Ligthert [MVP] <notmyfirstname @planet.nlwrote :
        An array is immutable, it means that at every (size) change a new one will
        be created and the old one be set ready for the GC. If this is about strings
        it is even more, because strings themselfes are as well immutable.
        Arrays aren't immutable - only the *size* can't change. You can still
        change the contents of the array. If arrays were immutable, you
        couldn't do:

        x[0] = "hello";

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: Array question.

          Jon,
          Arrays aren't immutable - only the *size* can't change. You can still
          change the contents of the array. If arrays were immutable, you
          couldn't do:
          >
          As I described, however you are right, I used the wrong word, however I am
          sure I am not the only one who uses this for this behaviour as well.

          Cor

          "Jon Skeet [C# MVP]" <skeet@pobox.co mschreef in bericht
          news:MPG.1fb500 3a36e2e4cb98d5c 0@msnews.micros oft.com...
          Cor Ligthert [MVP] <notmyfirstname @planet.nlwrote :
          >An array is immutable, it means that at every (size) change a new one
          >will
          >be created and the old one be set ready for the GC. If this is about
          >strings
          >it is even more, because strings themselfes are as well immutable.
          >
          Arrays aren't immutable - only the *size* can't change. You can still
          change the contents of the array. If arrays were immutable, you
          couldn't do:
          >
          x[0] = "hello";
          >
          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Göran Andersson

            #6
            Re: Array question.

            Cor Ligthert [MVP] wrote:
            Jon,
            >
            >Arrays aren't immutable - only the *size* can't change. You can still
            >change the contents of the array. If arrays were immutable, you
            >couldn't do:
            >>
            >
            As I described, however you are right, I used the wrong word, however I am
            sure I am not the only one who uses this for this behaviour as well.
            >
            Cor
            Please correct anyone you encounter that uses it incorrectly. The
            concept is tricky enough without incorrect and contradicting
            descriptions of it floating around.

            :)
            "Jon Skeet [C# MVP]" <skeet@pobox.co mschreef in bericht
            news:MPG.1fb500 3a36e2e4cb98d5c 0@msnews.micros oft.com...
            >Cor Ligthert [MVP] <notmyfirstname @planet.nlwrote :
            >>An array is immutable, it means that at every (size) change a new one
            >>will
            >>be created and the old one be set ready for the GC. If this is about
            >>strings
            >>it is even more, because strings themselfes are as well immutable.
            >Arrays aren't immutable - only the *size* can't change. You can still
            >change the contents of the array. If arrays were immutable, you
            >couldn't do:
            >>
            >x[0] = "hello";
            >>
            >--
            >Jon Skeet - <skeet@pobox.co m>
            >http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            >If replying to the group, please do not mail me too
            >
            >

            Comment

            • Martin Arvidsson \(Visual Systems AB\)

              #7
              Re: Array question.

              Thanx for the great explanation.

              Now i got it at 100%

              Regards
              Martin

              "Adam Clauss" <cabadam@tamu.e duskrev i meddelandet
              news:12kkt62j7v qql2f@corp.supe rnews.com...
              When you have the explicit array (string[]), anytime you pull an item out
              of the array, ex:
              string s = x[0];
              >
              you (and the compiler) can safely assume that x[0] is actually a string.
              ArrayList on the other hand holds "System.Obj ect" - basically ANY type.
              Thus, to retrieve an item, you would need to cast it. There are a couple
              ways to do this:
              string s = (string)x[0];
              string s = x[0] as string;
              >
              The first will throw an InvalidCastExce ption in the event that x[0] is NOT
              a string, so use it only if you are 100% completely positive the list only
              holds strings. The second will simply return a 'null' instead of throwing
              an exception.
              >
              Also - a string[] is a fixed size array. Once the array is created, you
              cannot add new items, or remove items. With ArrayList, it dynamically
              adjusts the size, so you can add/remove items as you wish.
              >
              In .NET 2.0, with the introduction of generics, you can use List<string>
              (or List<intor List<SomeObj-- whatever you need). This is the
              equivalent of ArrayList (in terms of being able to add/remove items) with
              the added advantage of not needing to cast items out of it.
              >
              --
              Adam Clauss
              >
              "Martin Arvidsson" <yeahright@ms.c omwrote in message
              news:O14trus$GH A.5060@TK2MSFTN GP02.phx.gbl...
              >Hi all you gurus!
              >>
              >Whats the biggest difference between using a string[] x; or an ArrayList
              >x;
              >>
              >When to use wich?
              >>
              >Regards
              >Martin
              >>
              >
              >

              Comment

              Working...