我是一名新的C#程序员,我想用Windows Forms编写程序,可以从文本框中获取邻接矩阵,然后返回矩阵中1的总和。我正在编写用于输入矩阵的代码,但我不知道如何汇总其组件。
string s = txtFirstgraph.Text;
string[] words = s.Split(',');
List<string[]> matrix = new List<string[]>();
for (int i = 0; i < words.Count(); i++)
{
if (txtFirstedge.Text[i] == '0' || txtFirstedge.Text[i] == '1' )
{
string[] temp = words[i].Split(' ');
matrix.Add(temp);
}
else
{
MessageBox.Show("you only can enter 0 or 1 for adjancy matrix please try again");
this.Close();
}
}
答案 0 :(得分:1)
如果没有上下文,你的代码绝对没有意义,但是对列表中的元素进行求和应该是非常简单的:
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
我要提出的一点是你的邻接矩阵应该是整数,而不是字符串。实际上,如果你愿意,它可以是布尔位字段。但是,无论如何,你的代码没有任何意义,所以也许你有理由这样做。