Samooramad
asked on
method
Hi experts,
I need a method that will go through the elements of an array of classes one by one and check its integer attribute against an entered number(parameter for the method).
the method should return all the elements of the array that have an integer equal to the entered integer.
I couldnt figure out how to work with the array
thank you
I need a method that will go through the elements of an array of classes one by one and check its integer attribute against an entered number(parameter for the method).
the method should return all the elements of the array that have an integer equal to the entered integer.
I couldnt figure out how to work with the array
thank you
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If your array is called "myArray" you could do:
public Integer [] getAllNumbers(Integer myArray, int number)
{
List list = new ArrayList();
for (int counter=0; counter<myArray.length; counter++)
{
if ( ((Integer) myArray[i]).intValue() == number)
{
list.add(myArray[i]);
}
}
return list.toArray();
}
If you use any other types just replace the "Integer" with the type you want.
public Integer [] getAllNumbers(Integer myArray, int number)
{
List list = new ArrayList();
for (int counter=0; counter<myArray.length; counter++)
{
if ( ((Integer) myArray[i]).intValue() == number)
{
list.add(myArray[i]);
}
}
return list.toArray();
}
If you use any other types just replace the "Integer" with the type you want.
ASKER
>>Do you mean that, or an array of objects?
I mean an array of a class I created.. it has integer attributes and strings and boolean ..
But this method should only invlove the integer
I mean an array of a class I created.. it has integer attributes and strings and boolean ..
But this method should only invlove the integer
>>I mean an array of a class I created..
OK. The code i posted should work then. 'comparedInt' would be the input to the method
OK. The code i posted should work then. 'comparedInt' would be the input to the method
ASKER
would those example work for that?
>>the method should return all the elements of the array that have an integer equal to the entered integer.
CEHJ
it should return the element of the array i.e instance
>>equalIntegers.add(new Integer(i));
equalIntegers.add(instance s[i]);
CEHJ
it should return the element of the array i.e instance
>>equalIntegers.add(new Integer(i));
equalIntegers.add(instance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A sample code:
Object [] myArray = new Object[3];
myArray[0] = "hey";
myArray[1] = new Boolean(true);
myArray[2] = new Integer(1);
Object [] resultArray = getAllNumbers(myArray, 1);
System.out.println("number s equal found:");
for (int counter=0; counter<resultArray.length ; counter++)
{
System.out.println(((Integ er) resultArray[i]).intValue() );
}
...
...
public Object [] getAllNumbers(Object myArray, int number)
{
List list = new ArrayList();
for (int counter=0; counter<myArray.length; counter++)
{
if (myArray[i] instanceof Integer)
{
if ( ((Integer) myArray[i]).intValue() == number)
{
list.add(myArray[i]);
}
}
}
return list.toArray();
}
Object [] myArray = new Object[3];
myArray[0] = "hey";
myArray[1] = new Boolean(true);
myArray[2] = new Integer(1);
Object [] resultArray = getAllNumbers(myArray, 1);
System.out.println("number
for (int counter=0; counter<resultArray.length
{
System.out.println(((Integ
}
...
...
public Object [] getAllNumbers(Object myArray, int number)
{
List list = new ArrayList();
for (int counter=0; counter<myArray.length; counter++)
{
if (myArray[i] instanceof Integer)
{
if ( ((Integer) myArray[i]).intValue() == number)
{
list.add(myArray[i]);
}
}
}
return list.toArray();
}
ASKER
>>OK. The code i posted should work then. 'comparedInt' would be the input to the method
Cehj my array isnt a list. Does that make any difference or would it still work?
Cehj my array isnt a list. Does that make any difference or would it still work?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>equalIntegers.add(new Integer(i));
Cehj this only returns the integer right? I need it to return the whole class with the matching integer
Cehj this only returns the integer right? I need it to return the whole class with the matching integer
ASKER
girionis do I just replace Object with my class in:
public Object [] getAllNumbers(Object myArray, int number)
???
public Object [] getAllNumbers(Object myArray, int number)
???
> I need it to return the whole class with the matching integer
The method I posted above will return all the objects that have matching integer attribute.
The method I posted above will return all the objects that have matching integer attribute.
> girionis do I just replace Object with my class in:
Yes you do, you will also need to do the appropriate casting when you receive the array.
Yes you do, you will also need to do the appropriate casting when you receive the array.
ASKER
>>The method I posted above will return all the objects that have matching integer attribute
yep that worked great objects :)
yep that worked great objects :)
ASKER
thank you everyone.. girionis answered first with what I wanted, so girionis should get the accepted answer :)
but everyones help is greatly appritiated
but everyones help is greatly appritiated
>>Cehj my array isnt a list. Does that make any difference or would it still work?
That doesn't matter. My example uses the source array and then adds to a List
>>the method should return all the elements of the array that have an integer equal to the entered integer.
You can amend my example to either
a. return the array indexes
List equalIntegers = new ArrayList();
int comparedInt = ...
for(int i = 0;i < instances.length;i++) {
int instanceInt = instances[i].getIntegerAtt ribute();
if (instanceInt == comparedInt) {
equalIntegers.add(new Integer(i));
}
}
System.out.println(equalIn tegers);
or
b. Add the object references to the List
List equalIntegerObjects = new ArrayList();
int comparedInt = ...
for(int i = 0;i < instances.length;i++) {
int instanceInt = instances[i].getIntegerAtt ribute();
if (instanceInt == comparedInt) {
equalIntegerObjects.add(in stances[i] );
}
}
That doesn't matter. My example uses the source array and then adds to a List
>>the method should return all the elements of the array that have an integer equal to the entered integer.
You can amend my example to either
a. return the array indexes
List equalIntegers = new ArrayList();
int comparedInt = ...
for(int i = 0;i < instances.length;i++) {
int instanceInt = instances[i].getIntegerAtt
if (instanceInt == comparedInt) {
equalIntegers.add(new Integer(i));
}
}
System.out.println(equalIn
or
b. Add the object references to the List
List equalIntegerObjects = new ArrayList();
int comparedInt = ...
for(int i = 0;i < instances.length;i++) {
int instanceInt = instances[i].getIntegerAtt
if (instanceInt == comparedInt) {
equalIntegerObjects.add(in
}
}
8-)
ASKER
thanks CEHJ:)
:)
Do you mean that, or an array of objects?