Вывести одномерный массив с неизвестным количеством элементов в listBox - C#
Формулировка задачи:
Доброй ночи всем, кто не спит.
Такое дело, нужно сравнить 2 матрицы и после равные соответствующие элементы вывести в вектор.
То есть если a11=b11, то это число записывается в вектор, если нет, то не записывается.
Воспользовался для сравнения методом GetVector, но получается, что невозможно заранее рассчитать количество элементов такого массива. Так как же его вывести?
Вот как у меня это все выглядит:
И вот на всякий случай код программы целиком. Реализована возможность случайной генерации матриц, ввода заранее заданных матриц (констант) и ввода с клавиатуры. Для всего юзаю dataGridView'ы
Помогите, если можете, я уже потерялся во всем этом.
static int[] GetVector(int[,] array1, int[,] array2) // Метод для сравнения матриц
{
if (array1.GetLength(0) != array2.GetLength(0) || array1.GetLength(1) != array2.GetLength(1))
return null;
List<int> vector = new List<int>();
for (int i = 0; i < array1.GetLength(0); i++)
for (int j = 0; j < array1.GetLength(1); j++)
{
if (array1[i, j] == array2[i, j])
vector.Add(array1[i, j]);
}
return vector.ToArray();
}
private void сравнитьМатрицыToolStripMenuItem_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
int n = dataGridView1.ColumnCount;
int m = dataGridView1.RowCount - 1;
int[,] a = new int[n, m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i, j] = Convert.ToInt32(dataGridView1[i, j].Value);
//
int n1 = dataGridView2.ColumnCount;
int m1 = dataGridView2.RowCount - 1;
int[,] b = new int[n1, m1];
for (int i = 0; i < n1; i++)
for (int j = 0; j < m1; j++)
b[i, j] = Convert.ToInt32(dataGridView1[i, j].Value);
int [] Vector = GetVector(a, b);
string wow;
wow = "";
for (int i = 0; i < n; i++)
{
wow += (Vector[i]).ToString() + " ";
}
listBox1.Items.Add(wow);
}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 Desantnik
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void случайноеЗаполнениеToolStripMenuItem_Click(object sender, EventArgs e) // Матрица a
{
int q1;
int m1;
q1 = Convert.ToInt32(textBox1.Text);
m1 = Convert.ToInt32(textBox2.Text);
Random rand = new Random();
double[,] mas = new double[q1, m1];
for (int q = 0; q < q1; q++)
{
for (int m = 0; m < m1; m++)
{
mas[q, m] = rand.Next(999);
}
dataGridView1.RowCount = q1;
dataGridView1.ColumnCount = m1;
int i, j;
for (i = 0; i < q1; ++i)
for (j = 0; j < m1; ++j)
dataGridView1.Rows[i].Cells[j].Value = mas[i, j];
}
}
private void констатнтыToolStripMenuItem_Click(object sender, EventArgs e) // Матрица a
{
for (int n = 0; n < 3; n++)
{
double [,] mas = new double[3, 4] { { 101, 202, 303, 909 }, { 323, 434, 545, 606 }, { 121, 202, 343, 969 } };
dataGridView1.RowCount = 3;
dataGridView1.ColumnCount = 4;
int i, j;
for (i = 0; i < 3; ++i)
for (j = 0; j < 4; ++j)
dataGridView1.Rows[i].Cells[j].Value = mas[i, j];
}
}
private void своиЗначенияToolStripMenuItem_Click(object sender, EventArgs e) // Матрица a
{
int n = dataGridView1.ColumnCount;
int m = dataGridView1.RowCount - 1;
int[,] a = new int[n, m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i, j] = Convert.ToInt32(dataGridView1[i, j].Value);
}
private void задатьРазмерностьToolStripMenuItem1_Click(object sender, EventArgs e) // Матрица a
{
dataGridView1.Columns.Clear();
dataGridView2.Columns.Clear();
int n = int.Parse(textBox1.Text)-1;
int m = int.Parse(textBox2.Text);
for (int i = 0; i < m; i++)
{
dataGridView1.Columns.Add("", "");
}
for (int j = 0; j < n; j++)
{
dataGridView1.Rows.Add();
}
dataGridView2.Columns.Clear();
for (int i = 0; i < m; i++)
{
dataGridView2.Columns.Add("", "");
}
for (int j = 0; j < n; j++)
{
dataGridView2.Rows.Add();
}
}
private void случайноеЗаполнениеToolStripMenuItem1_Click(object sender, EventArgs e) // Матрица b
{
int q1;
int m1;
q1 = Convert.ToInt32(textBox1.Text);
m1 = Convert.ToInt32(textBox2.Text);
Random rand = new Random();
double[,] mas = new double[q1, m1];
for (int q = 0; q < q1; q++)
{
for (int m = 0; m < m1; m++)
{
mas[q, m] = rand.Next(999);
}
dataGridView2.RowCount = q1;
dataGridView2.ColumnCount = m1;
int i, j;
for (i = 0; i < q1; ++i)
for (j = 0; j < m1; ++j)
dataGridView2.Rows[i].Cells[j].Value = mas[i, j];
}
}
private void константыToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int n = 0; n < 3; n++)
{
double[,] mas = new double[3, 4] { { 103, 221, 32, 903 }, { 323, 43, 545, 606 }, { 121, 212, 343, 965 } };
dataGridView2.RowCount = 3;
dataGridView2.ColumnCount = 4;
int i, j;
for (i = 0; i < 3; ++i)
for (j = 0; j < 4; ++j)
dataGridView2.Rows[i].Cells[j].Value = mas[i, j];
}
}
private void своиЗначенияToolStripMenuItem1_Click(object sender, EventArgs e) // Матрица b
{
int n = dataGridView2.ColumnCount;
int m = dataGridView2.RowCount - 1;
int[,] b = new int[n, m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
b[i, j] = Convert.ToInt32(dataGridView1[i, j].Value);
}
static int[] GetVector(int[,] array1, int[,] array2) // Метод для сравнения матриц
{
if (array1.GetLength(0) != array2.GetLength(0) || array1.GetLength(1) != array2.GetLength(1))
return null;
List<int> vector = new List<int>();
for (int i = 0; i < array1.GetLength(0); i++)
for (int j = 0; j < array1.GetLength(1); j++)
{
if (array1[i, j] == array2[i, j])
vector.Add(array1[i, j]);
}
return vector.ToArray();
}
private void сравнитьМатрицыToolStripMenuItem_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
int n = dataGridView1.ColumnCount;
int m = dataGridView1.RowCount - 1;
int[,] a = new int[n, m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i, j] = Convert.ToInt32(dataGridView1[i, j].Value);
//
int n1 = dataGridView2.ColumnCount;
int m1 = dataGridView2.RowCount - 1;
int[,] b = new int[n1, m1];
for (int i = 0; i < n1; i++)
for (int j = 0; j < m1; j++)
b[i, j] = Convert.ToInt32(dataGridView1[i, j].Value);
int [] Vector = GetVector(a, b);
string wow;
wow = "";
for (int i = 0; i < n; i++)
{
wow += (Vector[i]).ToString() + " ";
}
listBox1.Items.Add(wow);
}
}
}Решение задачи: «Вывести одномерный массив с неизвестным количеством элементов в listBox»
textual
Листинг программы
listBox1.DataSource = твойМассив;