检查数组包含元素

如果你有一个像current[x] = 0一个空的例子(假设你知道X)检查数组包含元素

如果你得到了很多的数字,例如:

123 

231

231

我编辑这些数的每一个我得到的数它转换为一个数组,如时间:

new[0] = 1 

new[1] = 2

new[2] = 3

我怎么能这些数字添加到当前的阵列是这样的:

current[0] = 1 

current[1] = 2

current[2] = 3

但现在,第二个新阵应该不重复相同的数字(IDK如何用言语所以这里说明的是图片:

所以如果你的图片添加号码,如,你会得到这个结尾:

current[0] = 1 

current[1] = 2

current[2] = 3

current[3] = 1

current[4] = 3

current[5] = 2

current[6] = 1

也许数组是错误的方式来做到这一点?

在此先感谢:)

回答:

让我改一下这个问题: 你有2所列出,你必须串联这样的名单:

newList = firstList.someHead() + firstList.someTail() + secondList.someTail() 

与条件:

firstList.someTail() == secondList.someHead() 

newList = firstList if firstList.contains(secondList) 

因此,您必须遍历这两个列表并找到完整匹配,例如其中一个itarators没有下一个

回答:

我的解决方案将使用字符串变量作为您的最终结果。无论何时开始使用contains()函数检查存在的新数字。从入站号码中逐个删除数字并使用contains()进行检查。您点击的那一刻在最终字符串的末尾添加删除的数字。

String strFinal = ""; 

String arrNo = "123";

String removedDigits = "";

for(int i = 0 ; i < arrNo.length() ; i++){

if(strFinal.contains(arrNo))

break;

else

{

removedDigits = arrNo.subString(arrNo.length() -2) + removedDigits; // removedDigits = 3, 23, 123

arrNo = arrNo.subString(0, arrNo.length() - 1); // arrNo = 12, 1, ""

}

}

strFinal += removedDigits; //123

以上是 检查数组包含元素 的全部内容, 来源链接: utcz.com/qa/266624.html

回到顶部