Solved

Java arrayList

Posted on 2013-11-22
7
241 Views
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.setTitle("Listing");
      frame.setSize(400, 400);
      frame.setLocationRelativeTo(null); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //allow x to exit
      frame.setVisible(true);
       
}
}

Open in new window

0
Comment
Question by:lostinspace9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 39669532
Just use the instanceof operator -  if(it.next() 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())
0
 
LVL 86

Accepted Solution

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

Expert Comment

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

 . . . which is a rather excellent point.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Closing Comment

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

Expert Comment

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

Expert Comment

by:CEHJ
ID: 39671337
:)
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39671339
:(
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

617 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