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 5 of 5

Thread: arraylist

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default arraylist

    i am making three classes student school and teststudent, in student class student name,subject age is defined and in school class student objects are created using arraylist, but in my program i am unable to access the method of student class,whats wrong with my code
    public class Student {
    String name;
    String subject;
    int age;
    Student(String name,String subject,int age){
    this.name = name;
    this.subject = subject;
    this.age = age;
    }
    public void setName(String name){
    this.name = name;
    }
    public String getName(){
    return this.name;
    }
    public String getSubject(){
    return this.subject;
    }
    public int getAge(){
    return this.age;
    }

    }
    import java.util.*;

    public class School {
    public ArrayList <Student> students = new ArrayList <Student>();
    public void addStudent(String name,String subject,int age){
    Student newStudent = new Student(name,subject,age);
    students.add(newStudent);
    }
    public void showSubject(String student){
    newStudent.getSubject();


    }

    }


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    My Mood
    Nerdy
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: arraylist

    You have defined the newStudent inside the addStudent() and you are accessing the newStudent in showSubject(). This is the root of problem.
    Either pass newStudent to showSubject() or declare newStudent at class level.

  3. The Following User Says Thank You to Bhawani For This Useful Post:

    ankit04 (January 6th, 2013)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: arraylist

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Smile Re: arraylist

    Hi ankit, Pls try this code to access the subject of perticular student.
    ..code removed
    Last edited by copeg; January 7th, 2013 at 11:24 AM. Reason: removed spoonfeeding

  6. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: arraylist

    @basha2013, please read the forum rules and the following:

    The Problem with Spoon-feeding

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 03:44 AM
  2. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM

Tags for this Thread