Home > Mobile >  C#: store and write coordinates in a Queue
C#: store and write coordinates in a Queue

Time:11-21

I am learning C#. I want to store x,y-coordinates in an queue. therefore I created an array. But I can't write it. Can anybody help me and tell me what I am doing wrong?

thank you very much!

public static void Main()
    {
        Random rnd = new Random();
        Queue<int[]> myQ = new Queue<int[]>();
        int plX = rnd.Next(0, 10);
        int plY = rnd.Next(0, 10);
        int[] arr1 = new int[] {plX, plY};
        int[] arr2 = new int[] {plX, plY};
        int[] arr3 = new int[] {plX, plY};
        int[] arr4 = new int[] {plX, plY};
        
        myQ.Enqueue(arr1);
        myQ.Enqueue(arr2);
        myQ.Enqueue(arr3);
        myQ.Enqueue(arr4);

        foreach (int[] point in myQ)
        {
            WriteLine(point);
        }
    }

CodePudding user response:

Replace your foreach loop with

foreach (var point in myQ)
{
    Console.WriteLine(point[0]);
    Console.WriteLine(point[1]);
}

values were saved, but you were printing type of stored value, not the actual values.

To do it in the way you were trying to do it, you would have to create custom class for the point with overwritten ToString() method:

using System;
using System.Collections.Generic;
                    
public class Program
{
    public static  void Main()
    {
        Random rnd = new Random();
        var myQ = new Queue<Point>();
        var arr1 = new Point(rnd.Next(0, 10), rnd.Next(0, 10));
        var arr2 = new Point(rnd.Next(0, 10), rnd.Next(0, 10));
        var arr3 = new Point(rnd.Next(0, 10), rnd.Next(0, 10));
        var arr4 = new Point(rnd.Next(0, 10), rnd.Next(0, 10));

        myQ.Enqueue(arr1);
        myQ.Enqueue(arr2);
        myQ.Enqueue(arr3);
        myQ.Enqueue(arr4);

        foreach (var point in myQ)
        {
            Console.WriteLine(point);
        }
    }

    public class Point
    {
        public int PlX {get; set;}
        public int PlY {get; set;}

        public Point(int x, int y)
        {
            PlX = x;
            PlY = y;
        }
        public override string ToString()
        {
            return PlX   ", "   PlY;
        }
    }
}

Mind that this is an example - it is very basic and unsafe (null checks etc...)

CodePudding user response:

You can use string interpolation:

foreach (var point in myQ)
{
    Console.WriteLine($"{point[0]} : {point[1]}");
}

Please, note var instead of int[]: you don't have to remember the exact type (often it's very long) and let compiler does it for you.

You can put it like this:

public static void Main()
{
    // First let's generate coordinates
    var rnd = new Random();
    int plX = rnd.Next(0, 10);
    int plY = rnd.Next(0, 10);

    // Then create queue
    var myQ = new Queue<int[]>(new int[][] {
        new int[] {plX, plY},
        new int[] {plX, plY},
        new int[] {plX, plY},
        new int[] {plX, plY},  
    });

    // Finally, print the queue
    foreach (var point in myQ)
    {
        Console.WriteLine($"{point[0]} : {point[1]}");
    }
}
  • Related