-1

I have a Class A

CLASS A

public ClassA {

ArrayList<ClassB> objB= new ArrayList<ClassB>();

 //getter and setter of objB

}

CLASS B

public classB {

int x;
int y;

private List<ClassC> objC= null;

//getter and setter of x,y and objC

}

CLASS C

public classC {

int a;
int b;

//getters and setters of a,b

}

I am storing a resultset in objB inside class A. I am trying to iterate objB in my service class.

public Class serviceImpl{

ClassA objA = new ClassA();

//here I need to get the values of variable x,y,a,b

}

3
  • 1
    Your question doesn't make much sense. Please state more clearly what you want... Commented Jul 10, 2014 at 21:20
  • 2
    Show your code instead of describing it. Commented Jul 10, 2014 at 21:20
  • possible duplicate of Iterating ArrayList inside a method Commented Jul 10, 2014 at 21:28

2 Answers 2

2

You could use and enhanced for-loop.

Don't use classB instead of B in the generics. You got it right during the second edition. Perhaps this can help you.

    ArrayList<B> classB = new ArrayList<B>();
public static void main(String[] args) {
    // instantiate the objects
    A myClassA = new A();
    B myClassB = new B();
    C myClassC1 = new C();
    C myClassC2 = new C();
    // set the values
    myClassC1.setA(1);
    myClassC1.setB(10);
    myClassC2.setA(2);
    myClassC2.setB(20);
    myClassB.setX(3);
    myClassB.setY(30);
    // create the lists
    List<C> myListC = new ArrayList<C>();
    myListC.add(myClassC1);
    myListC.add(myClassC2);
    // set the lists
    myClassB.setObjC(myListC);
    myClassA.classB.add(myClassB);// better use a getter/setter
    // iterate the list
    for (B auxClassB : myClassA.classB) {
        System.out.println("X:" + auxClassB.getX() + " Y:"
                + auxClassB.getY());
        for (C auxClassC : auxClassB.getObjC()) {
            System.out.println("A: "+  auxClassC.a + " B: "+ auxClassC.getB());
        }
    }
}

Console output

X:3 Y:30
A: 1 B: 10
A: 2 B: 20

Good luck

Sign up to request clarification or add additional context in comments.

5 Comments

Before anyone asks, no this is not a link-only answer.
Today I learned that in Java the foreach iterator is called an "enhanced for loop", even though it is less useful than regular for loops.
I know that, i'm just wating the question to develop
Hi @PbxMan, I have updated my question, hope it makes more sense now.
aah, i got it, thanks for making me understand. Appreciate your help :). thank you so much :)
0

Somewhere in class A:

for (ClassB b : classB)
{
    // whatever you want with b, which is of type ClassB
}

1 Comment

Class A is just a value object class which contains the arraylist object of class B and getters and setters. I need to get the content of Class B and C by creating object of class A in different class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.