1. Member
Join Date
Apr 2012
Posts
7
Rep Power
0

## Arrays element Equals

Hi

I can make equals of 2 array list.
if (Arrays.equals(data1, data2)) {

but how can i make equal of element in the array.

Write a method called contains that accepts two arrays of integers a1 and a2 as parameters and that returns a
boolean value indicating whether or not the sequence of elements in a2 appears in a1 (true for yes, false for no).
The sequence must appear consecutively and in the same order. For example, consider the following arrays:
int[] list1 = {1, 6, 2, 1, 4, 1, 2, 1, 8};
int[] list2 = {1, 2, 1};
The call of contains(list1, list2) should return true because the sequence of values in list2 {1, 2, 1} is contained
in list1 starting at index 5. If list2 had stored the values {2, 1, 2}, the call of contains(list1, list2)
would return false. Any two lists with identical elements are considered to contain each other. Every array contains
the empty array, and the empty array does not contain any arrays other than the empty array itself.

plz help
Last edited by must0995; 04-15-2012 at 05:11 PM.

2. ## Re: Arrays element Equals

Originally Posted by must0995
Hi

I can make equals of 2 arrays.
if (Arrays.equals(data1, data2)) {

but how can i make arrays of elements in the array.
Your assignment has nothing to do with this "making arrays of elements in the array". You simply would use for loops. Why not try to solve this and then if it doesn't work, show us what you've tried.

3. Member
Join Date
Apr 2012
Posts
7
Rep Power
0

## Re: Arrays element Equals

My friend

I cant solve becouse i do not have the experience of how to comare 2 element of one block eg.

{1,2,1} compre this with { 1,2,4,7, 1,2,1}

Only experience i got is using equals.

and i can only use it when i compare whole array list not the element.

eg. i can compare list{1,2,2,2} with list2{1,2,2,2}

by using if{arrays.equals{list,list1}

my quastion is:
how can i compare element element not array not array, how can i compre indside the array the element.

4. ## Re: Arrays element Equals

How do you access an element of an array? You use the [] notation with the name of the array and an index value:
theArrayName[theIndex]

5. Member
Join Date
Apr 2012
Posts
7
Rep Power
0

## Re: Arrays element Equals

i dont [inappropriate language deleted] understand this. i wasted now 5 hours on this quastion. and now i face 4 another quastion want me to compare a list of element with another list of array element.

Write a method called allLess that accepts two arrays of integers and returns true if each element in the first array
is less than the element at the same index in the second array. Your method should return false if the arrays are not
the same length

please understadn this. i do do do do not not not not have experience in java. i dont know whatt to useeeeeeee. and howwww to compare first element with another arrays element.

give me solution and i will solve all other quastion:
be fair.

1. i know i should use boolean.
2. i know i need loops
3. how the [inappropriate language deleted] can i make 2 loops goes indside 2 arrays . catch first element. then compare with another array's element.

this is not funny.
import java.util.Arrays;

public class k7Selfcheck13 {
public static void main(String[] args) {

int[] first = {1,2,3,4,5,6,7,8,9};
int[] seconed ={1,2,3,4,5,6,7,8,9};

allLess(first,seconed);

}

private static boolean allLess(int[] first, int[] seconed) {

boolean name = false;
for (int i = 0; i < first.length ; i++) {

for (int j = 0; j < seconed.length; j++) {

if(Arrays.equals(first,seconed)){

name = true;
}

}
}

return name;
}
}
Last edited by Fubarable; 04-15-2012 at 10:43 PM. Reason: inappropriate language and content

6. ## Re: Arrays element Equals

Given an array, how do you access an element of that array?
This is the way: theArrayName[theIndex]
Now what if there is another array that you want to compare the elements of one array against the elements of the other?
How would you code a comparison of values if the values to be compared were in variables? Say abc vs def?
if( abc < def)
The elements of an array are the same as variables and can be used the same way as a variable:
if(anarray[indexToArray] < def)

For more on arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

7. Member
Join Date
Apr 2012
Posts
1
Rep Power
0

## Re: Arrays element Equals

check this out
for ( int i = 0; int < (array.length-(shorter_array.length)); i++)
{
if(array[i]equals(shorter_array[i])
{
system.out.println("These two match"+array[i]);
}
}

Try somehting like this and you should be able to figure the rest out.

8. Member
Join Date
Apr 2012
Posts
7
Rep Power
0

## Re: Arrays element Equals

Originally Posted by Norm
Given an array, how do you access an element of that array?
This is the way: theArrayName[theIndex]
Now what if there is another array that you want to compare the elements of one array against the elements of the other?
How would you code a comparison of values if the values to be compared were in variables? Say abc vs def?
if( abc < def)
The elements of an array are the same as variables and can be used the same way as a variable:
if(anarray[indexToArray] < def)

For more on arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

ty man now i understand what u mean.

9. ## Re: Arrays element Equals

Originally Posted by zonic
... code deleted ...
Zonic, please don't spoon-feed answers here. It's a lazy way to answer as it's easy to create the codes, much easier than trying to explain to the poster how to fix it themselves, and the posters learn nothing but how to beg for code.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•