Results 1 to 9 of 9
Thread: Arrays element Equals
- 04-15-2012, 04:48 PM #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 helpLast edited by must0995; 04-15-2012 at 05:11 PM.
-
Re: Arrays element Equals
- 04-15-2012, 05:45 PM #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.
- 04-15-2012, 05:49 PM #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]If you don't understand my response, don't ignore it, ask a question.
- 04-15-2012, 07:56 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: Arrays element Equals
please help..
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
- 04-15-2012, 08:18 PM #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)If you don't understand my response, don't ignore it, ask a question.
- 04-15-2012, 08:33 PM #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.
- 04-15-2012, 10:34 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
-
Re: Arrays element Equals
Similar Threads
-
using .equals
By droidus in forum New To JavaReplies: 11Last Post: 01-21-2012, 04:33 AM -
c always equals a*b
By imorio in forum New To JavaReplies: 3Last Post: 11-12-2010, 03:32 PM -
== and equals()
By arefeh in forum New To JavaReplies: 13Last Post: 01-05-2010, 05:56 PM -
== is same as .equals()??
By DrMath in forum New To JavaReplies: 1Last Post: 09-30-2009, 05:57 AM -
name clash: equals(E) in and equals(java.lang.Object)
By AdRock in forum New To JavaReplies: 0Last Post: 01-26-2008, 12:13 AM
Bookmarks