PDA

View Full Version : مبتدی: تعریف آرایه بدون اندازه



f_g1348
یک شنبه 18 تیر 1391, 09:37 صبح
با سلام
دوستان چطور میشه در برنامه در ابتدا یک آرایه یک بعدی تعریف کرد و سپس در جاهای مختلف برنامه به آن اندازه و مقدار داد .
مثلاً در یک جا بگوییم 24 عنصر دارد و در جای دیگر آن مقادیر یا پاک شوند یا بمانند و طول آرایه بیشتر شود .
با تشکر

ahmadreza517
یک شنبه 18 تیر 1391, 09:53 صبح
به نام تنها برنامه نویس هستی
سلام ,

بهتر شما برای این کار از ArrayList استفاده کنید. اینا هم می شه استفاده کرد.
Hashtable
List

f_g1348
یک شنبه 18 تیر 1391, 10:03 صبح
میشه بیشتر توضیح بدی و یک مثال بیاری ؟

Sirwan Afifi
یک شنبه 18 تیر 1391, 12:54 عصر
اینجا (http://www.hamcodi.ir/qa/questions/518/%D8%AA%D8%B9%D8%B1%DB%8C%D9%81-%D8%A2%D8%B1%D8%A7%DB%8C%D9%87-%D8%A8%D8%A7-%D8%B7%D9%88%D9%84-%D9%85%D8%AA%D8%BA%DB%8C%D8%B1-%D8%AF%D8%B1-c) رو نگاه کنید

tooraj_azizi_1035
یک شنبه 18 تیر 1391, 12:58 عصر
سلام
در کلاس ArrayList مادامی که شما عناصر رو به آرایه اضافه می کنید اندازه به طور خودکار افزایش پیدا می کنه.
می تونید سایز رو با فراخوانی TrimToSize یا ست کردن علنی Capacity کاهش بدید.
ArrayList چندبعدی ساپورت نمیشه.


using System;
using System.Collections;
public class SamplesArrayList {

public static void Main() {

// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!");

// Displays the properties and values of the ArrayList.
Console.WriteLine( "myAL" );
Console.WriteLine( " Count: {0}", myAL.Count );
Console.WriteLine( " Capacity: {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
}

public static void PrintValues( IEnumerable myList ) {
foreach ( Object obj in myList )
Console.Write( " {0}", obj );
Console.WriteLine();
}

}


/*
This code produces output similar to the following:

myAL
Count: 3
Capacity: 4
Values: Hello World !

*/


تغییر سایز:


using System;
using System.Collections;
public class SamplesArrayList {

public static void Main() {

// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumped" );

// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "Initially," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );

// Trim the ArrayList.
myAL.TrimToSize();

// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "After TrimToSize," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );

// Clear the ArrayList.
myAL.Clear();

// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "After Clear," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );

// Trim the ArrayList again.
myAL.TrimToSize();

// Displays the count, capacity and values of the ArrayList.
Console.WriteLine( "After the second TrimToSize," );
Console.WriteLine( " Count : {0}", myAL.Count );
Console.WriteLine( " Capacity : {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
}

public static void PrintValues( IEnumerable myList ) {
foreach ( Object obj in myList )
Console.Write( " {0}", obj );
Console.WriteLine();
}

}
/*
This code produces the following output.

Initially,
Count : 5
Capacity : 16
Values: The quick brown fox jumped
After TrimToSize,
Count : 5
Capacity : 5
Values: The quick brown fox jumped
After Clear,
Count : 0
Capacity : 5
Values:
After the second TrimToSize,
Count : 0
Capacity : 16
Values:
*/

Sirwan Afifi
یک شنبه 18 تیر 1391, 13:06 عصر
نکته جالب در استفاده از ArrayList اینکه می تونیم هر شیء از نوع Object رو در اون ذخیره کنیم :

ArrayList arr=new ArrayList();
arr.Add("Hello");
arr.Add(12);
arr.Add(12.1);
.
.
.