|
Elementni shart bo‘yicha topish
|
bet | 54/154 | Sana | 08.01.2024 | Hajmi | 5,29 Mb. | | #131939 |
Bog'liq MajmuaElementni shart bo‘yicha topish:
using System;
class ArrayErr
{
static void Main()
{
var people = new string[] {"Ali", "Vali", "Abbos", "Nodir", "Salohiddin", "Oybek" , "Bekmurod" };
//satr uzunligi 4 ta belgidan katta bo‘lgan birinchi va oxirgi elementlarni topish
string first = Array.Find(people, rr => rr.Length > 4);
Console.WriteLine(first); // Abbos
string last = Array.FindLast(people, rr => rr.Length > 4);
Console.WriteLine(last); // Bekmurod
// satr uzunligi 5 ga teng elementlarni toping
string[] group = Array.FindAll(people, rr => rr.Length == 5);
foreach (var person in group) Console.Write(person+" ");
// Abbos Nodir Oybek
Console.ReadKey();
}
}
Elementlarning tartibini teskarisiga o‘zgartiramiz:
using System;
class ArrayErr
{
static void Main()
{
var people = new string[] {"Ali", "Vali", "Abbos", "Nodir", "Salohiddin", "Oybek" , "Bekmurod" };
Array.Reverse(people);
for (int i=0;i
Console.Write(people[i] +" ");
Console.WriteLine();
Array. Reverse (people, 1, 3);
for (int i=0;i
Console.Write(people[i] +" ");
Console.ReadKey();
}
}
Dastur natijasida quyidagi qiymatlar chiqadi
Bekmurod Oybek Salohiddin Nodir Abbos Vali Ali
Bekmurod Nodir Salohiddin Oybek Abbos Vali Ali
Massivlarda foreach sikli
C# da har qanday qiymatlar to‘plamining elementlarini takrorlash uchun ajoyib sikl ishlatiladi. Massiv elementlarini takrorlash uchun shartli va parametrli sikllardan foydalanish mumkin. Masalan, massivning elementlarini ko‘rsatish uchun quyidagicha yozish zarur:
for (int i = 0; i < sort.Length; i++)
Console.WriteLine(sotr[i]);
Biroq bunday kod biroz noqulay ko‘rinishi mumkin. Shuning uchun foreach siklidan foydalanish ancha oson:
|
| |