Home Discussion Discuss: Insert an array list of objects with addAll(ArrayList) method

  • need help with ArrayListbamiday October 27, 2011 at 7:51 PM

    It usefull if you can show us another example by using ArrayList in class child relating with class parent..and then manipulate them and display them in Main....I wish to know how that to get informations and also do calculating the data from the class Vehicule, voit, Camion, Camionnette...in a different way ....by declaring constructor Convoi(ArrayList vec, String nom).... I tried to work with this way but it's not run. I think that I didn't do right something..please help. Thank in advanced. import java.util.ArrayList; abstract class Vehicule {protected String immat; protected float poidVide=0f; protected int type; protected float charge=0f; Vehicule(String immat,float poidVide) { this.immat=immat; this.poidVide=poidVide; type=0; } public abstract int calVitMax(); public abstract String toString(); } class Voiture extends Vehicule { private int mVitesse; Voiture(String m, float n,float charge) {super(m,n); type=1; this.charge = charge; } public int calVitMax() { if((type==1)&&(poidVide==1)) mVitesse=150; else mVitesse=90; return mVitesse; } public String toString() { return "Immatricule :"+this.immat+" categorie : voiture, charge "+this.charge+"ton(s), poid vide:"+this.poidVide+" ton(s)"; } } class Camionnette extends Vehicule { private int mVitesse; Camionnette(String m, float n,float charge) {super(m,n); type=2; this.charge = charge; } public int calVitMax() { if(type==2&&poidVide<=1.5) { if(this.charge==0) mVitesse=130; if(this.charge<=1) mVitesse=110; if(this.charge>1) mVitesse=90; } else System.out.println("Invalid data, Veuillez entrer a value pour le charge < 1.5 tons"); return mVitesse; } public String toString() { return "Immatricule: "+this.immat+" categorie : Camionnnette , charge "+this.charge+" ton(s),poid vide:"+this.poidVide+" ton(s)"; } } class Camion extends Vehicule { private int maxVitesse; Camion(String m, float n,float charge) {super(m,n); type=3; this.charge = charge; } public int calVitMax() { if(type==3&&poidVide<4) { if(this.charge==0) maxVitesse=130; if(this.charge<=3) maxVitesse=110; if(this.charge>3) maxVitesse=90; } else System.out.println("Invalid data, please enter new value for a charge"); return maxVitesse; } public String toString() { return "Immatrucule: "+ this.immat+" categorie : camion, charge "+this.charge+" ton(s) poid vide:"+this.poidVide+" ton(s)"; } } class Convoi extends Vehicule { protected ArrayList<Vehicule> lesVehic; protected String nomConvoi; private int som; Convoi(ArrayList v, String nomConvoi) { lesVehic= new ArrayList<Vehicule>(); this.lesVehic = v; this.nomConvoi=nomConvoi; som=0; /*for(Vehicule vehic:lesVehic) vehic.add(lesVehic,nomConvoi);*/ } public String toString() { return this.nomConvoi+" "+this.lesVehic+" "; } public int VitesseMoyen(int vit) { som+=les.Vehic.calVitMax(vit); return som/lesVehic.size(); } } public class TestVehicule { public static void main(String args[]) { Vehicule[] vehicule = new Vehicule[6]; Voiture voit = new Voiture("ADR-45687",1f,1.5f); Camion camion = new Camion("ERT-78945",1.5f,3f); Camionnette camionnet = new Camionnette("RTC-453288",1.5f,1f); Voiture voit2 = new Voiture("DRt-68733",1f,1f); Camion camion2 = new Camion("TAZ-94221",1.5f,4f); Camionnette camionnet2 = new Camionnette("CQA-32844",1.5f,0f); vehicule[0] = voit; vehicule[1]= camion; vehicule[2]= camionnet; vehicule[3] = voit2; vehicule[4]= camion2; vehicule[5]= camionnet2; for(int i=0;i<6;i++) {System.out.println(vehicule[i].toString()); System.out.println("max vitesse: "+vehicule[i].calVitMax()+" km/h"); } ArrayList<Convoi> convoi = new ArrayList<Convoi>(Voiture v,"AER-77865"); for(Vehicule vehicule:lesVehic) { convoi.toString(); convoi.VitesseMoyen(135); } } }

Post your Comment



Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Related Articles
General

Insert an array list of objects with addAll(ArrayList) method
Insert an array list of objects with addAll(ArrayList) method In this section you will learn to insert an array list of objects to another list...) method.   Learn how to use the addAll method of the List interface. Example 

General

Java ArrayList Addall
It adds all the elements of the second list at the end of the given first list. Java Arraylist Addall Example import†java.util.*; public...","pakistan","United†Kingdom","Japan"}; Listlist=new†ArrayList(); list.add(ar 

Java-Tutorials

Arraylist in java
) - Insert the element at the specified position in the list boolean addAll...ArrayList is a class that extends AbstractList and implements List Interface.... but for indefinite number of elements Arraylist is used as it creates dynamic Array 

Java Beginners

arraylist
ArraylistException { void buildAlphabet() { ArrayList list = new ArrayList...")); } System.out.print("Elements of array are: " + list); } public static void main...arraylist   Using arraylist class , what type of exception are throws 

JSP Servlet Questions

How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?
How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?  I have a jsp page containing a list box and a text box. I am sending an Array List object from a servlet to this jsp 

Java Beginners

Array list in Java - arraylist type error
Array list in Java - arraylist type error  I am using Array list in Java but when i compile i get array list type error. Can anyone please explain what is array list type error and why it occurs 

General

Java arraylist to array
Java arrayList has method toArray() It converts the given list... be used properly. Example of Java Arraylist to Array import...)†{ †††††int†ar[]={444,222,333,111}; †††††ArrayListlist=new†ArrayList 

Java Beginners

ArrayList programe
ArrayList programe  How to write a java program to accept an array list of Employee objects. search,delete and modify a particular Employee based on Id Number (like ID,Name&Address 

Java Beginners

array list
array list  How to get repeate occurence values from database table in java by using arraylist 

Java Interview Questions

maximum size of arraylist - java
^31-1. 2)Inserting an element in the middle of the array list and removing an element from the middle of the array list are not efficient since the arraylist... System.arrayCopy method which is a native method to copy the contents from old array to new 

General

Java arraylist to string array
arrayList can be converted into string array. For this first converts the arrayList into the object array. This is done by  using the toArray() method. Then cast it into the string and assign in the String array 

General

Java arraylist of objects
In arrayList object is added by default This can be object of String, Integer or any Wrapper class Example of Java Arraylist of Objects import†java.util.ArrayList; import†java.util.List; public†class 

Java Interview Questions

Array List
Array List   Complete the class House and HouseApp below to display... to insert 4 houses√Ę‚?¨‚?Ę information into the ArrayList // display the houses√Ę‚...{ public static void main(String []args){ // declare two ArrayList named 

Java Tips and Examples

Convert ArrayList To Array
arrays to be viewed as list. asList(): asList() is a method of Array class... Convert ArrayList To Array      ... will learn how to convert an Arraylist into an array.  Code Description 

Java Beginners

Array List
; // prompt user to insert 4 houses‚?? information into the ArrayList // display... price and insert the house information into another ArrayList. Display...Array List  Could somebody help me to solve this question. I am glad 

Java Tips and Examples

Sorting Array List in Java
Sorting Array List in Java In this example you will learn about sorting array list in java using an example. By default array list are sorted as the element are inserted into the list. In Java ArrayList extends AbstractList implements 

Java-Tutorials

Shuffling the Element of a List or Array
Shuffling the Element of a List or Array   ... the list or array to prepare each and every element for the operation. In this section, you will learn about shuffling the element of a list or array. Shuffling 

Java Beginners

Array list java code - Java Beginners
Array list java code  Create an employee class with an employee id's,name & address. Store some objects of this class in an arraylist. When an employee id is given, display his name & address? Please provide the DETAIL java code 

General

Convert List to ArrayList
Convert List to ArrayList      ... to ArrayList.  Code Description: This program helps you in converting List to ArrayList.  Here we have taken a List of Colors or you can also take 

Java Beginners

Java ArrayList Example
Java ArrayList Example  How can we use array list in java program... be dynamically resized. Here, we have created an array list and store string values to it. The method size() of ArrayList class returns the size of the arraylist 

Java Interview Questions

Array list java code - Java Interview Questions
Array list java code  Create an employee class with an employee id's,name & address. Store some objects of this class in an arraylist. When... and address,how can we store in arraylist, and how we can display his name 

General

Java ArrayList, Java Array List examples
ArrayList list = new ArrayList(); // Adding Element to the ArrayList Object...The Java ArrayList (java.util.ArrayList) is resizable implementation of the List interface. It has support for all the functions present in the List interface 

Java Interview Questions

Array List and string operation
Array List and string operation  hi, I want to search an arraylist element in a a given file. example I have a name called "mac" in my arraylist and I have a txt file which contains mac, how many times "mac" appears in the txt 

Java Beginners

arraylist in java
arraylist in java  arraylist in java   Array List Example in java 

Java Beginners

How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... class EmpList { public static Set getList(List<Employe> list){ Employe emp 

Java Beginners

How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... class EmpList { public static Set getList(List<Employe> list){ Employe emp 

Java Beginners

How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... class EmpList { public static Set getList(List<Employe> list){ Employe emp 

General

Java arraylist merge
. Use addAll() method to add list2 into the list1. Now the list2...}; ††††††††List†list1=new†ArrayList(); ††††††††List†list2=new†ArrayList... In Java Two arrays can be joined by using the Collection list 

Java Interview Questions

Array list java program - Java Interview Questions
Array list java program  Create an employee class with an employee id's,name & address. Store some objects of this class in an arraylist. When... and address variables..how we can store objects of that class in arraylist and how 

Java Magazine Questions

Question in Create Arraylist ?
stores list of √Ę??PhoneEntry√Ę?? objects. Use an array of size (MAXSIZE = 1000) to hold the list. Notes: Add appropriate data fields to the class √...Question in Create Arraylist ?  Good Morning EveryOne I have Q 

DMCA.com