Solved

Java arrayList

Posted on 2013-11-22
7
240 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
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!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tomcat: Unable to run tomcat service. 2 34
jboss wildfly 10.1 10 617
eclipse console opening separately 2 49
Html split(text) 2 49
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

739 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