public class Search
{
public static void main(String[] args)
{
int idx = -1;
int len = 1000;
int abc[] = new int[] { 1, 2, 3, 4, 5 }; int[] nums = new int[len];
for (int i = 0; i < len; i++)
nums[i] = (int)(Math.random() * 10);
for (int i = 0; i < len; i++)
{
System.out.print(nums[i]);
System.out.print("\t");
if (i % 10 == 9) System.out.println();
}
}
}这个用for循环要怎么实现呢
------解决方案--------------------
- Java code
public class Search { public static void main(String[] args) { int idx = -1; int len = 100000; int abc[] = new int[] { 1, 2, 3, 4, 5 }; int[] nums = new int[len]; for (int i = 0; i < len; i++) nums[i] = (int) (Math.random() * 10); //nums[10] = 1;nums[11]=2;nums[12]=3;nums[13]=4;nums[14]=5; //System.out.println(java.util.Arrays.toString(nums)); for (int i = 0; i < len-abc.length; i++) { for(int j=i,k=0;k<abc.length;j++,k++) { if(nums[j] != abc[k]) { break; } System.out.println(k); if(k == (abc.length - 1)){ System.out.println("gg"+(j-abc.length)); System.exit(0); } } } }}
------解决方案--------------------
利用HashSet第一次添加返回true,再次为false的特性。