Java arrayList

Posted on 2013-11-22
Last Modified: 2013-11-23
I want to be able to loop through my arraylist and determine which class it came from. I want to use that value for things further.

public class test extends JFrame{
    int [] people ;

    public test(){
	  Person [] people = new Person[4];
	  float grades[] = {90,90,91,92,92,93,93,94,94,95,96,96,95,95,95};
          ArrayList<Person> list = new ArrayList<Person>(4);
	  list.add(new Teacher("Teacher ", "One", 420));
	  list.add(new Student("Student ", "One", 11111, grades));
          list.add(new Teacher("Teacher ", "Two", 421));
	  list.add(new Student("Student ", "Two", 22222, grades));
          setLayout(new GridLayout(4, 2, 5, 5));
          for (int i=0; i< list.size(); ++i){
              add(new JTextField("" + list.get(i)), BorderLayout.WEST);

      public static void main(String[] args){
      test frame = new test(); 
      frame.setSize(400, 400);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //allow x to exit

Open in new window

Question by:lostinspace9
  • 4
  • 2
LVL 16

Expert Comment

ID: 39669532
Just use the instanceof operator -  if( instanceof Student){do smthg;}
Of course you won't be able to do that for the Person class, as they are all people.

(Iterator "it" = list.iterator())
LVL 86

Accepted Solution

CEHJ earned 500 total points
ID: 39669616
Why put teachers and students in the same list in the first place?
LVL 16

Expert Comment

ID: 39669654
Why put teachers and students in the same list in the first place?

 . . . which is a rather excellent point.
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.


Author Closing Comment

ID: 39670748
Thanks again for the rather simple tip.......Just been staring at this thing too long..
LVL 16

Expert Comment

ID: 39671014
Hmm. Right answer to the wrong question.
LVL 86

Expert Comment

ID: 39671337
LVL 16

Expert Comment

ID: 39671339

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now