Menu
Coddy logo textTech

Kümeler Üzerinde Yineleme

Coddy'nin C# Journey'sinin Mantık & Akış bölümünün bir parçası. Ders 66 / 66.

Bir HashSet üzerinde yineleme yapmak, kümedeki her öğeye erişmenizi sağlar. C# dilinde, bir HashSet üzerinde yineleme yapmak için çeşitli yaklaşımlar kullanabilirsiniz.

Bir foreach döngüsü kullanarak:

// Bir HashSet oluştur
HashSet<string> colors = new HashSet<string>();

// HashSet'e öğeler ekle
colors.Add("Red");
colors.Add("Green");
colors.Add("Blue");

// HashSet üzerinde yinele
foreach (string color in colors)
{
    Console.WriteLine(color);
}

HashSet'in ekleme sırasını korumadığını unutma; bu nedenle öğeler eklendikleri sırayla yinelenmeyebilir.

Ayrıca önce bir diziye veya listeye dönüştürebilirsiniz:

// Convert to array and iterate
string[] colorArray = colors.ToArray();
for (int i = 0; i < colorArray.Length; i++)
{
    Console.WriteLine(colorArray[i]);
}
challenge icon

Görev

Kolay

Girdi olarak bir tamsayı HashSet'i alan PrintSetElements adlı bir metot oluşturun. Metot, küme üzerinde yineleme yapmalı ve her öğeyi yeni bir satırda yazdırmalıdır.

Örneğin, küme [5, 2, 8] içeriyorsa metodunuz şunları yazdırmalıdır:

5 2 8

Kendin dene

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Program
{
    public static void PrintSetElements(HashSet<int> set)
    {
        // Kodunuzu buraya yazın
    }
    
    static void Main(string[] args)
    {
        HashSet<int> numbers = new HashSet<int>();
        
        // Read first line to check format
        string firstLine = Console.ReadLine();
        
        // Check if input is in JSON array format
        if (firstLine != null && firstLine.StartsWith("[") && firstLine.EndsWith("]"))
        {
            try
            {
                // Extract content between square brackets
                string arrayContent = firstLine.Substring(1, firstLine.Length - 2);
                
                // Try to parse the JSON array content
                string[] values = Regex.Split(arrayContent, @",\s*");
                foreach (string value in values)
                {
                    // Remove any quotes if present
                    string cleanValue = value.Trim();
                    if (cleanValue.StartsWith("\"") && cleanValue.EndsWith("\""))
                    {
                        cleanValue = cleanValue.Substring(1, cleanValue.Length - 2);
                    }
                    
                    // Parse as integer and add to set
                    if (int.TryParse(cleanValue, out int num))
                    {
                        numbers.Add(num);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error parsing JSON input: {ex.Message}");
                return;
            }
        }
        else
        {
            try
            {
                // Traditional format - read count then elements
                int count = int.Parse(firstLine);
                
                // Read each element and add to the set
                for (int i = 0; i < count; i++)
                {
                    int num = int.Parse(Console.ReadLine());
                    numbers.Add(num);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error parsing traditional input: {ex.Message}");
                return;
            }
        }
        
        PrintSetElements(numbers);
    }
}
quiz iconKendini test et

Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.

Mantık & Akış bölümündeki tüm dersler

Kendi başına pratik yap: Online C# derleyicisi