[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

method

Posted on 2004-08-16
25
Medium Priority
?
322 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:Samooramad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 5
  • +2
25 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11808440
>>array of classes

Do you mean that, or an array of objects?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 11808447
If the latter, something like:

List equalIntegers = new ArrayList();
int comparedInt = ...
for(int i = 0;i < instances.length;i++) {
      int i = instances[i].getIntegerAttribute();
      if (i == comparedInt) {
            equalIntegers.add(new Integer(i));
      }
}
System.out.println(equalIntegers);
0
 
LVL 35

Expert Comment

by:girionis
ID: 11808452
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Samooramad
ID: 11808461
>>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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11808468
>>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
0
 

Author Comment

by:Samooramad
ID: 11808469
would those example work for that?
0
 
LVL 6

Expert Comment

by:expertmb
ID: 11808473
>>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(instances[i]);
0
 
LVL 35

Accepted Solution

by:
girionis earned 1000 total points
ID: 11808484
With a few modifications this should also work:

public Integer [] getAllNumbers(Integer 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();
}
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 1000 total points
ID: 11808487
And change this:

> public Integer [] getAllNumbers(Integer myArray, int number)

to this:

public Integer [] getAllNumbers(Object myArray, int number)

0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 1000 total points
ID: 11808489
public Object [] getAllNumbers(Object myArray, int number)
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 200 total points
ID: 11808493
>>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

List list = new ArrayList();
int number = somevalue;
for(int i = 0;i < instances.length;i++) {
if(instances[i] instanceof Integer){
  if ( ((Integer) instances[i]).intValue() == number)
        {
            list.add(instances[i]);
        }
 }
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 11808508
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("numbers equal found:");
for (int counter=0; counter<resultArray.length; counter++)
{
    System.out.println(((Integer) 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();
}
0
 

Author Comment

by:Samooramad
ID: 11808510
>>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?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 11808533
this should be helpful to you:

http://java.sun.com/docs/books/tutorial/java/data/arrays.html

public MyClass[] getAllNumbers(MyClass[] myArray, int number)
{
    List list = new ArrayList();

    for (int counter=0; counter<myArray.length; counter++)
    {
            if (myArray[i].getIntAttribute() == number)
            {
                list.add(myArray[i]);
            }
        }
    }

    return (MyClass[]) list.toArray(new MyClass[0]);
}
0
 

Author Comment

by:Samooramad
ID: 11808539
>>equalIntegers.add(new Integer(i));

Cehj this only returns the integer right? I need it to return the whole class with the matching integer
0
 

Author Comment

by:Samooramad
ID: 11808548
girionis do I just replace Object with my class in:

public Object [] getAllNumbers(Object myArray, int number)
???
0
 
LVL 92

Expert Comment

by:objects
ID: 11808550
> 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.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11808555
> 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.
0
 

Author Comment

by:Samooramad
ID: 11808572
>>The method I posted above will return all the objects that have matching integer attribute
yep that worked great objects :)
0
 

Author Comment

by:Samooramad
ID: 11808590
thank you everyone..  girionis answered first with what I wanted, so  girionis should get the accepted answer :)
but everyones help is greatly appritiated
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11808610
>>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].getIntegerAttribute();
      if (instanceInt == comparedInt) {
            equalIntegers.add(new Integer(i));
      }
}
System.out.println(equalIntegers);

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].getIntegerAttribute();
      if (instanceInt == comparedInt) {
            equalIntegerObjects.add(instances[i]);
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11808634
8-)
0
 

Author Comment

by:Samooramad
ID: 11808640
thanks CEHJ:)
0
 
LVL 92

Expert Comment

by:objects
ID: 11808647
0
 
LVL 35

Expert Comment

by:girionis
ID: 11808700
:)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question