admin管理员组文章数量:1355754
Current Outputs:
Is [1, 2, 3] found in [1, 2, 1, 2, 3] ? >>> false
Is [1, 1, 3] found in [1, 2, 1, 2, 3] ? >>> false
Needed Outputs:
Is [1, 2, 3] found in [1, 2, 1, 2, 3]? >>> true
Is [1, 1, 3] found in [1, 2, 1, 2, 3]? >>> false
This is an example of a general output of the code, it needs appear consecutively and in the same order. I will show my code, all I want is guidance and teaching, no cheating due to that being academic dishonesty. I hope to find some tasteful insight, and furthermore get my code running properly.
Java Code:
public static boolean contains(int[] a, int[] b)
{
int acounter = 0;
int bcounter = 0;
boolean tempcounter = true;
while (acounter < a.length)
{
if (a[acounter] == b[bcounter])
{
if (bcounter == b.length - 1)
return true;
bcounter += 1;
tempcounter = true;
}
else
{
if (tempcounter && acounter > 0)
{
acounter -= 1;
}
bcounter = 0;
tempcounter = false;
}
acounter += 1;
}
return false;
}
Main Method:
import java.util.*;
public class PracticeProblems
{
public static void main(String[] args)
{
int[] a = {1,2,3};
int[] b = {1,2,1,2,3};
int[] c = {1,1,3};
int[] d = {1,2,1,2,3};
System.out.println("\n\nIs " + Arrays.toString(a) + " found in " + Arrays.toString(b) + " ? " + ">>> " + contains(a, b));
System.out.println("Is " + Arrays.toString(c) + " found in " + Arrays.toString(d) + " ? " + ">>> " + contains(c, d) );
}
}
Current Outputs:
Is [1, 2, 3] found in [1, 2, 1, 2, 3] ? >>> false
Is [1, 1, 3] found in [1, 2, 1, 2, 3] ? >>> false
Needed Outputs:
Is [1, 2, 3] found in [1, 2, 1, 2, 3]? >>> true
Is [1, 1, 3] found in [1, 2, 1, 2, 3]? >>> false
This is an example of a general output of the code, it needs appear consecutively and in the same order. I will show my code, all I want is guidance and teaching, no cheating due to that being academic dishonesty. I hope to find some tasteful insight, and furthermore get my code running properly.
Java Code:
public static boolean contains(int[] a, int[] b)
{
int acounter = 0;
int bcounter = 0;
boolean tempcounter = true;
while (acounter < a.length)
{
if (a[acounter] == b[bcounter])
{
if (bcounter == b.length - 1)
return true;
bcounter += 1;
tempcounter = true;
}
else
{
if (tempcounter && acounter > 0)
{
acounter -= 1;
}
bcounter = 0;
tempcounter = false;
}
acounter += 1;
}
return false;
}
Main Method:
import java.util.*;
public class PracticeProblems
{
public static void main(String[] args)
{
int[] a = {1,2,3};
int[] b = {1,2,1,2,3};
int[] c = {1,1,3};
int[] d = {1,2,1,2,3};
System.out.println("\n\nIs " + Arrays.toString(a) + " found in " + Arrays.toString(b) + " ? " + ">>> " + contains(a, b));
System.out.println("Is " + Arrays.toString(c) + " found in " + Arrays.toString(d) + " ? " + ">>> " + contains(c, d) );
}
}
Share
Improve this question
edited Mar 31 at 10:41
Stephen C
720k95 gold badges846 silver badges1.3k bronze badges
asked Mar 31 at 8:45
NotInferno_NotInferno_
11 bronze badge
New contributor
NotInferno_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
- Normally a method like contains(a, b) should be read as 'a contains b'. So that means a is the larger array and b is the sub-array that might or might not be found in a. I think you have this the wrong way round in your example code. – Adriaan Koster Commented Mar 31 at 9:39
4 Answers
Reset to default 2Your logic uses a single loop in which it juggles with indexes. That is complicated if not incomprehensible.
Look for the algorithm, the logic. What would you do manually?
part: 1+2+3-
| 1-2 3
| | 1+2+3+!
all: 1 2 1 2 3
So in one loop over the larger array you would shift the smaller array inside the larger.
And inside the loop you would need to match elements of the smaller array, a second loop over the smaller array.
I think this is the basic algorithm:
boolean contains(int[] a, int[] b) {
int indexA = 0
int indexB = 0
while (indexA < a.length) {
if (a[indexA] != b[indexB]) {
indexB = 0
}
if (a[indexA] == b[indexB]) {
indexB++
if (indexB == b.length) {
return true;
}
}
indexA++
}
return false;
}
The 'trick' is to first check the reset condition. That is, when matches are being found, but then a mismatching value is encountered, and the indexB has to be reset to 0.
By doing this first, we achieve that after the reset, the same mismatching value of a is still tried as a possible start value for matching b.
If you analyze the code below these lines, you'll see that it does exactly the same thing as the code provided by Adriaan Koster. I think presented this way, it's easier to interpret.
The outer loop determines from which position in the containing array we will search for matches with the contained array. If a mismatch is found, "break" is called, and the check is repeated from the next position. If the end of the inner for is reached without calling "break," we return "true."
If the outer for reaches its end, we return "false".
public boolean contains( int[] arrA, int[] arrB ) {
for( int i = 0; i < arrB.length; i ++ ) {
for( int j = 0; j < arrA.length; j ++ ) {
if( arrB[ i + j ] != arrA[ j ] ) {
break;
}
if( j == arrA.length - 1 ) {
return true;
}
}
}
return false;
}
public static boolean contains(int[] a, int[] b)
{
int left = 0;
int right = a.length-1;
while(right<b.length)
{
boolean flag = false;
int temp = left;
while(temp<=right)
{
if(a[temp-left]!=b[temp])
{
flag = true;
break;
}
else temp++;
}
if(!flag) return true;
right++;
left++;
}
return false;
}
本文标签:
版权声明:本文标题:Needed Aid With Java Schoolwork - Containing Arrays & Array Searchs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743959224a2568709.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论