using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyFirstCSharp_01
{
public partial class Chap11_Array_test2 : Form
{
public Chap11_Array_test2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//배열 생성
int[,] iArray = new int[2, 4] { { 1, 2, 3, 4 }, // { 5, 6, 7, 8 }
{ 5, 6, 7, 8 } }; // { 1, 2, 3, 4 }
// 행의 수 구하기
int iArrayRowCount = iArray.GetLength(0);
// 열의 수 구하기
int iArrayColumnCount = iArray.GetLength(1);
// 1. 자리가 바뀐 데이터가 담길 배열 변수 생성
int[,] iResultArray = new int[iArrayRowCount, iArrayColumnCount];
// 2. 원본 데이터 행 역행하여 복사 배열에 담기]
int iArrayx = 0;
for (int x = iArrayRowCount - 1; x >= 0; x--)
{
for (int y = 0; y < iArrayColumnCount; y++)
{
// 행을 바꾸어 새로운 배열에 담기
iResultArray[iArrayx, y] = iArray[x, y];
}
++iArrayx;
}
// 텍스트 박스에 담기
string aArrayList = string.Empty;
textBox1.Text = "---------- 2차원 배열 행 바꾸기 ----------\r\n";
int iColumnCount = 0;
foreach(int iResult in iResultArray)
{
aArrayList += $"{iResult},";
if (++iColumnCount == iArrayColumnCount)
{
aArrayList = $"{{ {aArrayList} }} \r\n";
textBox1.Text += aArrayList;
aArrayList = "";
iColumnCount = 0;
}
}
}
}
}
my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyFirstCSharp_01
{
public partial class Chap11_Array_test : Form
{
public Chap11_Array_test()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
int[,] aArray = new int[2, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
int iArrayRowCount = aArray.GetLength(0); //행
int iArrayColumnCount = aArray.GetLength(1); //열
string sArrayList = string.Empty;
for (int x = (iArrayRowCount - 1); x >= 0; x--)
{
for (int y = 0; y < iArrayColumnCount; y++)
{
sArrayList += $"{aArray[x, y]}, ";
}
textBox1.Text += sArrayList + "\r\n";
sArrayList = "";
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
int[,] aArray = new int[2, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
int iArrayRowCount = aArray.GetLength(0); //행
int iArrayColumnCount = aArray.GetLength(1); //열
string sArrayList = string.Empty;
for (int x = 0; x < iArrayRowCount; x++)
{
for (int y = 0; y < iArrayColumnCount; y++)
{
sArrayList += $"{aArray[x, y]}, ";
}
textBox1.Text += sArrayList + "\r\n";
sArrayList = "";
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
LIST
'study' 카테고리의 다른 글
[C#] Chapter 11. Pairs with Sum of 16 (test) (0) | 2022.06.03 |
---|---|
[C#] Chapter 11. Array (0) | 2022.06.03 |
[C#] Chapter 11. Array (test) (0) | 2022.06.02 |
[C#] Chapter 11. Array (0) | 2022.06.02 |
[C#] Chapter 10. Loop - Foreach (0) | 2022.06.02 |