Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java arrayList

Posted on 2013-11-22
7
Medium Priority
?
245 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 17

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 2000 total points
ID: 39669616
Why put teachers and students in the same list in the first place?
0
 
LVL 17

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

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

Expert Comment

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

Expert Comment

by:CEHJ
ID: 39671337
:)
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 39671339
:(
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …
Suggested Courses

877 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