c锐利的二维阵列 [英] 2-D array in c sharp

查看:91
本文介绍了c锐利的二维阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



请帮我以下代码: -



Dear All,

Kindly help me in below code:-

Using System;

namespace ArrayApplication
{
    Class MyArray
    {
        static void Main(string[] args)
        int[,] a = new int [5,2] {{0,0},{1,2},{2,4},{3,6},{4,8}};

        int i,j;

        for(i=0;i<5;i++)
        {
            for(j=0;j<2;j++)
            {
                textbox1.text=a[i,j].toString();
            }
        }
    }
}





以上代码给出结果8即上面数组中的最后一个值。

我想要成为{0} {0} = 5

a {0} {1} = 2

........

........

a {4} {1} = 8



请帮助我。



above code gives result 8 that is last value in above array.
I want out to be as a{0}{0}=5
a{0}{1}=2
........
........
a{4}{1}=8

Kindly help me.

推荐答案

一个文本框只保存一个值 - 所以你的循环用下一个值覆盖每个先前的值,并且你只能看到最后一个元素。

你可以做你想要的,但这意味着在你进行时组装一个字符串。您可以使用字符串,但StringBuilder更有效:

A textbox only holds one value - so your loop is overwriting each previous value with the next one, and you only get to see the final element looked at.
You can do what you want, but it means assembling a string as you go along. You can use a string, but a StringBuilder is more efficient:
int[,] a = new int[5, 2] { { 0, 0 }, { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 } };
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++)
    {
    for (int j = 0; j < 2; j++)
        {
        sb.AppendFormat("a{{{0}}}{{{1}}} = {2}\r\n", i, j, a[i,j]);
        }
    }
textBox1.Text = sb.ToString();


尝试
Textbox1.text += "a[" + i + "][" + j + "]" + a[i][j] + "\n";

请尝试如下。



Please try is as below.

public static void Main()
    {
        int[,] a = new int [5,2] {{0,0},{1,2},{2,4},{3,6},{4,8}};

        int i,j;

        for(i=0;i<5;i++)
        {
            for(j=0;j<2;j++)
            {
               Console.WriteLine("Element({0},{1})={2}", i, j, a[i, j]);
            }
        }
    }





输出





OUTPUT

Element(0,0)=0
Element(0,1)=0
Element(1,0)=1
Element(1,1)=2
Element(2,0)=2
Element(2,1)=4
Element(3,0)=3
Element(3,1)=6
Element(4,0)=4
Element(4,1)=8





LIVE演示: ideone


这篇关于c锐利的二维阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆