Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java arrayList

Posted on 2013-11-22
7
Medium Priority
?
242 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 2000 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

670 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