Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reducing the size of an Array

Reducing the size of an Array

Scheduled Pinned Locked Moved C#
questiondata-structures
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adam Turner
    wrote on last edited by
    #1

    If i create an array that is a specific size

    int[] myInts = new int[10]

    but only fill the first 5 values, how can i reduce the length of the array to 5? so the following will happen

    myInts[0] = 5;
    myInts[1] = 4;
    myInts[2] = 3;
    myInts[3] = 2;
    myInts[4] = 1;
    for (int i = 0; i < myInts.Length; i++)
    {
    Console.WriteLine(myInts[i].ToString());
    }

    and the output would only be 5, 4, 3, 2, 1 and not throw an exception since myInts[5] is null

    F A 2 Replies Last reply
    0
    • A Adam Turner

      If i create an array that is a specific size

      int[] myInts = new int[10]

      but only fill the first 5 values, how can i reduce the length of the array to 5? so the following will happen

      myInts[0] = 5;
      myInts[1] = 4;
      myInts[2] = 3;
      myInts[3] = 2;
      myInts[4] = 1;
      for (int i = 0; i < myInts.Length; i++)
      {
      Console.WriteLine(myInts[i].ToString());
      }

      and the output would only be 5, 4, 3, 2, 1 and not throw an exception since myInts[5] is null

      F Offline
      F Offline
      firat kocak
      wrote on last edited by
      #2

      hi, if the size of the array is unknown before it is used then you must check each member of the array for null value. Change tour for loop to this for( int i = 0; i < nyInts.Length, myInts[i] != null; i ++ ) as i know there is no any function to resize of an array. cheers, Doing something is better than doing nothing. So ... Move !

      1 Reply Last reply
      0
      • A Adam Turner

        If i create an array that is a specific size

        int[] myInts = new int[10]

        but only fill the first 5 values, how can i reduce the length of the array to 5? so the following will happen

        myInts[0] = 5;
        myInts[1] = 4;
        myInts[2] = 3;
        myInts[3] = 2;
        myInts[4] = 1;
        for (int i = 0; i < myInts.Length; i++)
        {
        Console.WriteLine(myInts[i].ToString());
        }

        and the output would only be 5, 4, 3, 2, 1 and not throw an exception since myInts[5] is null

        A Offline
        A Offline
        Arjan Einbu
        wrote on last edited by
        #3

        If you need to resize an array, you should go throug the ArrayList, even though it isn't typed. If you need a typed array, but don't know the size of it you can use ArrayList in this way: ArrayList al = new ArrayList(); al.add(5); al.add(4); al.add(3); al.add(2); al.add(1); int[] myInts = (int[])al.ToArray(typeof(int)); If you need to change the size of the array, you can start with ArrayList al = new ArrayList(myInts); instead. This will add some overhead (going back and forth between arrays and arraylists), but it gives you the type safety you need.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups