长篇小说初学者程序员在这里用java练习。现在我正在使用两个数组,我想知道int arrayA(7,14,21,28)是否连续到int ArrayB意味着arrayB在数组中连续有(7,14,21,28)。如果是boolean则返回true,否则它将返回false。这是我的代码示例。
public class Harrison7aTest
{
public static void main(String [] args)
{
int[] arrayA = {7,14,21,28};
int[] arrayB = {1,3,5,7,14,21,28,32};
boolean result = false;
for(int A = 0; A < arrayA.length - 1; A++)
{
for(int B = 0; B < arrayB.length - 1; B++)
{
}
}
}
}
答案 0 :(得分:3)
您可以将它们转换为字符串并使用str.contains()
方法
String strArrA = Arrays.toString(arrayA);
String strArrB = Arrays.toString(arrayB);
//to strip square brackets that's comes with Arrays.toString
//for ex: Arrays.toString(arrayA); returns "[7, 14, 21, 28]"
//we want to convert it to "7, 14, 21, 28"
strArrA = strArrA.substring(1, strArrA.length()-1);
if (strArrB.contains(strArrA)) {
System.out.println("true");
} else {
System.out.println("false");
}
答案 1 :(得分:1)
如果您想使用该方法,您可能会在生产中用作Java工程师,然后检查@RC给出的重复链接。或@Raman的答案。出于作业目的,如果您只想使用单个循环回答问题,请考虑我的答案。您可以在arrayB
上进行一次迭代,并检查arrayA
中包含的数字序列是否发生,而不会中断。
public static boolean containsConsecutive(int[] a, int[] b) {
int aIndex = 0;
for (int i=0; i < arrayB.length; i++) {
if (aIndex != 0 && arrayB[i] != arrayA[aIndex]) {
break;
}
else if (arrayB[i] == arrayA[aIndex]) {
++aIndex;
}
}
return aIndex == arrayA.length;
}
public static void main(String[] args) {
int[] a = {7,14,21,28,32};
int[] b = {1,3,5,7,14,21,28,32};
// true
System.out.println(containsConsecutive(a, b));
a = {7,14,21,28,32};
b = {1,3,5,7,8,9,10,14,21,28,32};
// false - sequence not in order
System.out.println(containsConsecutive(a, b));
a = {7,14,21,28,32,35};
b = {1,3,5,7,14,21,28,32};
// false - entire sequence in a not contained within b
System.out.println(containsConsecutive(a, b));
}
答案 2 :(得分:1)
int[] arrayA = {7,14,21,28};
int[] arrayB = {1,3,5,7,14,21,28,32};
boolean result=false;
for(int i=0;i<arrayB.length;i++){
if(arrayA[0] == arrayB[i]){
for(int j=0;j<arrayA.length;j++){
if(arrayA[j] == arrayB[i+j]){
result=true;
}
else{
result=false;
}
}
}
}
System.out.println(result);
更新了一个:
public class Test {
public static void main(String[] args) {
int[] arrayA = { 7, 14, 21, 28 };
int[] arrayB = { 1, 3, 5, 7, 14, 21, 28, 32, 7 };
boolean output = Test.appearsConsecutive(arrayA,arrayB);
System.out.println(output);
}
public static boolean appearsConsecutive(int[] arrayA, int[] arrayB) {
boolean result = false;
for (int i = 0; i < arrayB.length; i++) {
if (arrayA[0] == arrayB[i]) {
for (int j = 0; j < arrayA.length; j++) {
if (arrayA[j] == arrayB[i + j]) {
result = true;
break;
} else {
result = false;
}
}
}
}
System.out.println(result);
return result;
}
}
参见上面的例子。
答案 3 :(得分:0)
如果有人需要算法答案....(我已经颠倒了arrayA和arrayB分配)。
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int[] arrayB = {7,14,21,28};
int[] arrayA = {1,3,5,7,14,21,28,32};
boolean result = false;
for(int A = 0; A < arrayA.length; A++)
{
if( arrayA[A] != arrayB[0]) continue;
//else ...
for(int B = 0; B < arrayB.length; B++)
{
if(arrayA[A] != arrayB[B] || A>=arrayA.length)
break;
if(B+1 == arrayB.length)
{
result = true;
break;
}
A++;
}
if(result)
break;
}
System.out.println("Contains :"+ result);
}