Creating an array

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

    Creating an array

    Hi,

    I new to C#, and I would like to know how I can create an array that I do
    not know in advance how many item will be placed there

    --
    Thank You in Advance
    Merci a l'avance

    Martin

  • Jeroen Mostert

    #2
    Re: Creating an array

    Martin Racette wrote:
    I new to C#, and I would like to know how I can create an array that I
    do not know in advance how many item will be placed there
    >
    Use ArrayList in .NET 1 or List<Tin .NET 2 (or any other more appropriate
    collection). You can think of those as "growable arrays".

    --
    J.

    Comment

    • Rene

      #3
      Re: Creating an array

      Maybe this will help?

      class Program
      {
      static void Main(string[] args)
      {
      // Get an array of ints.
      int[] i = GetSomeIntArray <int>(123);

      // Get an array of objects.
      object[] o = GetSomeIntArray <object>(456) ;
      }

      private static T[] GetSomeIntArray <T>(int size)
      {
      return new T[size];
      }
      }


      "Martin Racette" <rat7_2001@hotm ail.comwrote in message
      news:25873FA1-924C-4E6E-96EF-A63ECE5DD715@mi crosoft.com...
      Hi,
      >
      I new to C#, and I would like to know how I can create an array that I do
      not know in advance how many item will be placed there
      >
      --
      Thank You in Advance
      Merci a l'avance
      >
      Martin

      Comment

      • Rudy Velthuis

        #4
        Re: Creating an array

        Martin Racette wrote:
        Hi,
        >
        I new to C#, and I would like to know how I can create an array that
        I do not know in advance how many item will be placed there
        Say you have the number of items in numItems, and you want an array of
        integers, then you can do:

        int[] myArray = new int[numItems];

        --
        Rudy Velthuis http://rvelthuis.de

        "The secret of success is to know something nobody else knows."
        -- Aristotle Onassis (1906-1975)

        Comment

        • Martin Racette

          #5
          Re: Creating an array

          As I was saying I do not know how many items there will be in the array, so
          I can not specify the size of it, because the size might be 0 and then again
          it might be 1 000 000 000 000 items

          So is it possble to create an array without defining its size

          "Rudy Velthuis" <newsgroups@rve lthuis.dewrote in message
          news:xn0flb0g5c hps0200u@news.m icrosoft.com...
          Martin Racette wrote:
          >
          >Hi,
          >>
          >I new to C#, and I would like to know how I can create an array that
          >I do not know in advance how many item will be placed there
          >
          Say you have the number of items in numItems, and you want an array of
          integers, then you can do:
          >
          int[] myArray = new int[numItems];
          >
          --
          Rudy Velthuis http://rvelthuis.de
          >
          "The secret of success is to know something nobody else knows."
          -- Aristotle Onassis (1906-1975)
          --
          Thank You in Advance
          Merci a l'avance

          Martin

          Comment

          • Rudy Velthuis

            #6
            Re: Creating an array

            Martin Racette wrote:
            As I was saying I do not know how many items there will be in the
            array, so I can not specify the size of it, because the size might be
            0 and then again it might be 1 000 000 000 000 items
            Oh, you want it to be able to grow? Use a List<Tinstead.

            FWIW, you will probably have problems with 1 trillion items, no matter
            what data type you choose. <g>
            --
            Rudy Velthuis http://rvelthuis.de

            "I think it would be a good idea."
            -- Mahatma Gandhi (1869-1948), when asked what he thought of
            Western civilization

            Comment

            • Willy Denoyette [MVP]

              #7
              Re: Creating an array

              "Martin Racette" <rat7_2001@hotm ail.comwrote in message
              news:BE2021E7-D051-4809-9FBE-F5394248649E@mi crosoft.com...
              As I was saying I do not know how many items there will be in the array,
              so I can not specify the size of it, because the size might be 0 and then
              again it might be 1 000 000 000 000 items
              >
              So is it possble to create an array without defining its size
              This was answered by others, use an ArrayList or Lis<T>, note that you won't
              be able to store an array of 1 000 000 000 000 items ever, but I guess you
              know that too.

              Willy.

              Comment

              • Martin Racette

                #8
                Re: Creating an array

                Thanks, that was exactly what I was looking for

                And the number was just an example <g>

                "Rudy Velthuis" <newsgroups@rve lthuis.dewrote in message
                news:xn0flc8u8d osloh009@news.m icrosoft.com...
                Martin Racette wrote:
                >
                >As I was saying I do not know how many items there will be in the
                >array, so I can not specify the size of it, because the size might be
                >0 and then again it might be 1 000 000 000 000 items
                >
                Oh, you want it to be able to grow? Use a List<Tinstead.
                >
                FWIW, you will probably have problems with 1 trillion items, no matter
                what data type you choose. <g>
                --
                Rudy Velthuis http://rvelthuis.de
                >
                "I think it would be a good idea."
                -- Mahatma Gandhi (1869-1948), when asked what he thought of
                Western civilization
                --
                Thank You in Advance
                Merci a l'avance

                Martin

                Comment

                • Martin Racette

                  #9
                  Re: Creating an array

                  Thanks, that was exactly what I was looking for

                  And the number was just an example <g>

                  "Willy Denoyette [MVP]" <willy.denoyett e@telenet.bewro te in message
                  news:e6fK9xeWIH A.4880@TK2MSFTN GP03.phx.gbl...
                  "Martin Racette" <rat7_2001@hotm ail.comwrote in message
                  news:BE2021E7-D051-4809-9FBE-F5394248649E@mi crosoft.com...
                  >As I was saying I do not know how many items there will be in the array,
                  >so I can not specify the size of it, because the size might be 0 and then
                  >again it might be 1 000 000 000 000 items
                  >>
                  >So is it possble to create an array without defining its size
                  >
                  This was answered by others, use an ArrayList or Lis<T>, note that you
                  won't be able to store an array of 1 000 000 000 000 items ever, but I
                  guess you know that too.
                  >
                  Willy.
                  >
                  --
                  Thank You in Advance
                  Merci a l'avance

                  Martin

                  Comment

                  • =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

                    #10
                    Re: Creating an array

                    Rene wrote:
                    Maybe this will help?
                    >
                    class Program
                    {
                    static void Main(string[] args)
                    {
                    // Get an array of ints.
                    int[] i = GetSomeIntArray <int>(123);
                    >
                    // Get an array of objects.
                    object[] o = GetSomeIntArray <object>(456) ;
                    }
                    >
                    private static T[] GetSomeIntArray <T>(int size)
                    {
                    return new T[size];
                    }
                    }
                    This must surely be a solution to a different problem than what OP is
                    asking for?

                    1. it is named GetSome*Int*Arr ay, whereas it will happily return an
                    array of any type, since its generic
                    2. you can easily replace the whole method with a subset of its
                    contents, ie:

                    int[] i = new int[123];
                    object[] o = new object[456];

                    What in particular was the point of this method?

                    --
                    Lasse Vågsæther Karlsen
                    mailto:lasse@vk arlsen.no
                    Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

                    PGP KeyID: 0xBCDEA2E3

                    Comment

                    Working...