Results 1 to 8 of 8
  1. #1
    hassaanid2012 is offline Member
    Join Date
    Apr 2012
    Location
    Pakistan
    Posts
    20
    Rep Power
    0

    Default [SOLVED] Access ArrayList from another class!!!

    Hello Friends!!!

    I am new to Java. My java program has two classes (Student class and Project class) and both classes have one ArrayList. Student records are stored in Student ArrayList and Projects records are stored in Project ArrayList but now i want to create a method in Project class for Adding Projects to existing Students and i want to implement check in this method via if-else statement for checking if student exists then add projects to that student and if not exist then display the message but i have no idea how to access the Student ArrayList that is located in Student class to Project class for checking. Any help is greatly appreciated!!! :)
    Last edited by hassaanid2012; 04-15-2012 at 03:01 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Access ArrayList from another class!!!

    To access data in a class, you need to define methods in that class that can be call from methods in other classes. A reference to the class is needed to call its methods.
    Java Code:
    class Class1 {
      void method1() {}
    }
    
    class Class2 {
      Class1 cls1 = new Class1();
      void aMethod() {
         cls1.method1();  // call method in other class
      }
    }
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    hassaanid2012 is offline Member
    Join Date
    Apr 2012
    Location
    Pakistan
    Posts
    20
    Rep Power
    0

    Default Re: Access ArrayList from another class!!!

    Thanks for your help.....It works but now i am stuck in another problem..... :( Can you please tell me how to apply if-else statement on Empty ArrayList. I mean if ArrayList contains no element then how to display specific message by using if-else? Thanks!!!

  4. #4
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Access ArrayList from another class!!!

    Java Code:
    if(myArrayList.size() == 0)

  5. #5
    hassaanid2012 is offline Member
    Join Date
    Apr 2012
    Location
    Pakistan
    Posts
    20
    Rep Power
    0

    Default Re: Access ArrayList from another class!!!

    Thanks 4 ur reply mwr1976 but this won't work. Let me explain what i want to do.....I have Student ArrayList in which we can store students information and Project ArrayList in which we can store students projects information. I want to create a search method that checks the student existence and if student exist in Student ArrayList then Add Projects to existed student (Projects must store on Project ArrayList) and if student exist but no Projects are assigned to student then display the message like "No projects found for stuedent". That's what i want to do? Please help me to solve this problem. Any help is greatly appreciated...... :)

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Access ArrayList from another class!!!

    Take your list of requirements and make a list in pseudo code as a design for the code you want to write.
    Then write the java code from that design.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Access ArrayList from another class!!!

    Well describe what approach you would use... you should easily come to a solution, just think about it. You have an array of students... you look for a student... why don't you loop through all the students of that array?
    I think the next step is yours to do now...

  8. #8
    hassaanid2012 is offline Member
    Join Date
    Apr 2012
    Location
    Pakistan
    Posts
    20
    Rep Power
    0

    Default Re: Access ArrayList from another class!!!

    Thanks to all 4 ur help. I completed my program successfully. Your help is greatly appreciated......This thread is now closed :)

Similar Threads

  1. Replies: 2
    Last Post: 02-13-2012, 06:39 AM
  2. How to access an ArrayList in another class?
    By Alpa in forum New To Java
    Replies: 9
    Last Post: 12-30-2011, 11:30 PM
  3. Replies: 1
    Last Post: 11-04-2011, 07:14 AM
  4. Replies: 0
    Last Post: 04-04-2010, 10:06 AM
  5. How can i store ArrayList objects in Access database
    By frankycool in forum Advanced Java
    Replies: 3
    Last Post: 11-04-2009, 06:55 AM

Posting Permissions

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