Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
package javaapplication10arraylistandobjects;


import java.util.ArrayList;
import java.util.*;

public class JavaApplication10Arraylistandobjects {


    public static void main(String[] args) {
test();

    }

    
     public static void test(){
         ArrayList<flights> mi = new ArrayList();
         flights p = new flights("Jony","iris",23);
         flights p1 = new flights("Jony","Kogan",21);
         mi.add(p);
         mi.add(p1);
         
         for (int i=0;i<mi.size();i++){
         System.out.println(mi.get(i));
         }
     }
}


-=Class=-
Java
package javaapplication10arraylistandobjects;


public class flights {


    String f_name;
    String s_name;
    int age;


flights(String f_name,String s_name,int age){
        this.f_name = f_name;
        this.s_name = s_name;
        this.age = age;
    }


}



OUTPUT :

run:
javaapplication10arraylistandobjects.flights@4e25154f
javaapplication10arraylistandobjects.flights@70dea4e
BUILD SUCCESSFUL (total time: 0 seconds)

What I have tried:

No ideas at all.
I would like to know where's my mistake
Posted
Updated 27-Mar-18 15:39pm

1 solution

There are nothing wrong in your code. If you want to display all the item in the ArrayList, you probably need to add a toString() method to your flights class and just let the toString() method of ArrayList do all the work.

flights
public String toString(){
   return f_name+" "+s_name+" "+age;
JavaApplication10Arraylistandobjects
for (int i = 0; i < mi.size(); i++) {
             System.out.println(mi.get(i).toString());
   }
 
Share this answer
 
Comments
CPallini 28-Mar-18 3:57am    
5.
wseng 28-Mar-18 4:47am    
thanks sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900