如何比较2个数组.. [英] How To Compare between 2 arrays..
问题描述
你好..
Am遇到比较2个数组值的问题..
我需要的输出是:
如果text =hi
那么新数组将是这样的:
注意改变的地方是: - 前2个元素.. 。
{h,i,a,b,c,d,e,f,g,j, K, L, M, N, O, p, q, R, S, T, U, v,W ,x,y,z};
我的代码做的每件事情都正确但我的问题是当我想从数组元素移位首先到(文本框中的文本)结束的位置..
代码使用的是:
static 字符串 [] arrayofstring = { a, b,< span class =code-string> c, d, e, f, g,< span class =code-string> h, i, j, k, l, m, n, o , p, q, r , s, t, u, v, w , x, y, z };
字符串 [] arraywithkey = new string [arrayofstring.Length];
private void btn_Click(对象发件人,EventArgs e)
{
// 阅读文本框并将其值保存到名为arraywithkey的新数组中
int txtlength = txttext.Text.Length - 1 跨度>;
for ( int i = 0 ; i < = txtlength; i ++)
{
string s = txttext.Text.Substring(i, 1 );
arraywithkey [i] = s;
}
// 将检查数组的所有值
for ( int j = 0 ; j < = 25 ; j ++)
{
// 检查新数组的值并向其添加新值
for ( int h = txtlength + 1; h < = < span class =code-digit> 25 ; h ++)
{
// 如果找到值,则比较2个数组跳过其他向新数组添加值
if (arraywithkey [j] == arrayofstring [j])
{
break ;
}
else
{
MessageBox.Show( h是: + h);
MessageBox.Show( j是: + j);
arraywithkey [h] = arrayofstring [j];
}
}
}
}
提前感谢...
如果我没错,实际上你需要根据提供的数据交换数组在文本框中。
这样文本框中的数据将在数组中排在第一位。
您可以尝试下面给出的内容。
字符串 [] arrayofstring = { a, b, c, d, e, f, g, h, i, j , k, l, m, n, < span class =code-string> o, p, q, r, s, t, u, v, w, x, y, z};
字符串 [] arraywithkey = new string [arrayofstring.Length];
string text = txttext.Text; // hi;
String [] tmpArrary = new String [text.Length];
for ( int i = 0 ; i < text.Length; i ++)
{
string s = text.Substring(i, 1 );
tmpArrary [i] = s;
}
arraywithkey = tmpArrary.Union(来自 s arrayofstring where !tmpArrary.Contains(s) select s)。ToArray();
hello ..
Am Facing a problem with comparing 2 arrays values ..
my output that i need is :
if the text = " hi"
then the new array will be like this :
note where the change is :- first 2 elements ...
{"h","i","a","b","c","d","e","f","g","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
my code do every thing right but my problem is when i want to shift array elements from the first to where the (text from textbox ) ends ..
the code am using is :
static String[] arrayofstring = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
String[] arraywithkey = new string[arrayofstring.Length];
private void btn_Click(object sender, EventArgs e)
{
// reading textbox and save it's value into new array with name arraywithkey
int txtlength = txttext.Text.Length - 1;
for (int i = 0; i <= txtlength; i++)
{
string s = txttext.Text.Substring(i, 1);
arraywithkey[i] = s;
}
// will check all values of the array
for (int j = 0; j <= 25; j++)
{
// check values of the new array and add new values to it
for (int h = txtlength+1 ; h <= 25; h++)
{
// compare 2 arrays if value found skip else add value to new array
if (arraywithkey[j] == arrayofstring[j])
{
break;
}
else
{
MessageBox.Show("h is :"+h);
MessageBox.Show("j is :"+j);
arraywithkey[h] = arrayofstring[j];
}
}
}
}
thanks in advance ...
Hi,
If I am not wrong, actually you need to swap array according to data provided in textbox.
Such that data in text box will be first in array.
You can try like below given.
String[] arrayofstring = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; String[] arraywithkey = new string[arrayofstring.Length]; string text = txttext.Text; // "hi"; String[] tmpArrary = new String[text.Length]; for (int i = 0; i < text.Length; i++) { string s = text.Substring(i, 1); tmpArrary[i] = s; } arraywithkey = tmpArrary.Union(from s in arrayofstring where !tmpArrary.Contains(s) select s).ToArray();
这篇关于如何比较2个数组..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!