Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: ArrayLists and Classes

  1. #1
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy ArrayLists and Classes

    Hi, I have this piece of code, which adds marks into 2 different arrayLists, one for homework marks, and one for examination marks..


     
    ArrayList<Double> homeworkMark = new ArrayList<Double>();
    ArrayList<Double> examinationMark = new ArrayList<Double>();
     
    boolean markCheck = true;
            do{
           //the purpose of this try catch is to make sure that the entered mark is a valid number. If the program encounters an exception markCheck will become true and the loop will begin again asking the user to enter the marks. If the marks are all encountered correctly markCheck will be false and the loop will end.
            try{
             for (int i = 1; i <= amountAssignment; i++ ) {
               sc.reset();
               System.out.print("Homework Mark for Assignment " +(i) + ": ");
               homeworkMark.add(sc.nextDouble());
               System.out.print("Examination Mark for Assignment " + (i) + ": ");
               examinationMark.add(sc.nextDouble());
               markCheck = false;
     
             }
           }catch (Exception e) {
             e.printStackTrace();
             System.out.println("------------");
             System.out.println("|  ERROR   |");
             System.out.println("------------");
             System.out.println("You'll have to enter your marks again!") ;
             System.out.println("");
             markCheck = true;
     
           }
          }while (markCheck == true);


    I need to add the data from this arrayList in the Main method into an arrayList in a different class, named subject, which can found below...

     
    import java.util.ArrayList;
    public class subject
    {
       int subjectNumber;
     
     
        public subject(int subjectNumber) {
          this.subjectNumber = subjectNumber;  
     
       }
     
       public int getSubjectNumber() {
           return subjectNumber; 
        }
     
       void setSubjectNumber (int subNum) {
            subjectNumber= subNum;
       }
     
    }

    I have no idea how to do this and I've tried multiple things but cannot seem to get anywhere... :-(

    Regards


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ArrayLists and Classes

    One way to get data from another class is to add a getter method in the class with the data and use that in the method in another class to access the data in the class with the data.

    arrayList in the Main method
    If the arraylists are defined inside of a method, a getter method will not be able to access it. The arraylist needs to be defined as an instance member of the class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: ArrayLists and Classes

    I don't disagree with anything Norm said, but you said, "I need to add the data from this arrayList in the Main method into an arrayList in a different class," which is slightly different. You could create a method in the other class, Subject, called (for example), addListItems( ArrayList items ), that adds the argument ArrayList's members to the ArrayList in an instance of Subject. Then in main(), you would do something like:

    Subject subject = new Subject();

    subject.addListItems( mainArrayList );

    where mainArrayList is the ArrayList in main() that you want to add to the other class' ArrayList.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: ArrayLists and Classes

    You've continued this topic in another thread. I'm not merging them, because I think it would be confusing, so I'll just close this one.

Similar Threads

  1. PLEASE HELP WITH JAVA ARRAYLISTS! Thanks.
    By javahelpneeded in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2014, 05:54 PM
  2. Classes of classes? [Very beginner asking a question] [w/ Codes]
    By UhKindaAwkward in forum Object Oriented Programming
    Replies: 6
    Last Post: December 24th, 2013, 03:05 AM
  3. boolean arraylists?
    By Scorks in forum Collections and Generics
    Replies: 1
    Last Post: October 15th, 2013, 11:47 AM
  4. [SOLVED] ArrayLists - Getting index of parameters of objects of ArrayLists.
    By mwebb in forum Object Oriented Programming
    Replies: 1
    Last Post: February 16th, 2012, 07:39 PM
  5. Creating ArrayLists
    By Khadafi in forum Java Theory & Questions
    Replies: 15
    Last Post: January 12th, 2012, 06:18 PM