Array länge beschneiden



  • Hallo zusammen,

    ich habe ein Array von einer länge von 100. Nun möchte ich dem Array aber sagen dass es letztenendes nur noch 50 lang sein soll.

    kann ich die länge eines array's im nachhinein ändern ?



  • Hi,

    Du kannst 'Array.Resize' dafür verwenden. Dabei gehen allerdings die Einträge verloren. Lösung ist, ein neues Array mit 50 Einträgen zu erstellen und die 'Array.Copy'-Methode zu verwenden.



  • Bei 'Array.Resize' gehen keine Daten verloren, es sei denn die neue ist kleiner als die alte, dann wird natürlich abgeschnitten.

    Microsoft schrieb:

    This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.

    If newSize is greater than the Length of the old array, a new array is allocated and all the elements are copied from the old array to the new one. If newSize is less than the Length of the old array, a new array is allocated and elements are copied from the old array to the new one until the new one is filled; the rest of the elements in the old array are ignored. If newSize is equal to the Length of the old array, this method does nothing.


Anmelden zum Antworten