Solved

Java arrayList

Posted on 2013-11-22
7
237 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 160
diffSum example 4 36
servlet example 11 40
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 33
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

776 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