Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Wrapper-class Insert an array list of objects with addAll(ArrayList) method

Related Tutorials


 
 

Share on Google+Share on Google+

Insert an array list of objects with addAll(ArrayList) method

Advertisement
In this section you will learn to insert an array list of objects to another list. Here is an example that provides the usage of the addAll(Arrayist) method in more detail.

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.

Here is an example that provides the usage of the addAll(Arrayist) method in more detail.

 

Create a class "addAll" with an ArrayList . Populate it with the object using  the addAll(ArrayList) method.

 

Learn how to use the addAll method of the List interface. Example code discussed here will tell you how to do this.

Here is the Code of the Example :

 

 

 

 

AddAll.java:

import java.util.*;
public class addAll{
  public static void main(String argv[]){
  List v = new ArrayList();
  v.addAll(Arrays.asList(new Integer[]{1,2,3,4}));
  for(int i=0; i < v.size();i ++){
  Integer iw =(Integerv.get(i);
  System.out.println(iw);
  }
  }
}

Here is the Output of the Example :

C:\roseindia>javac VecAdd.java

C::\roseindia>java VecAdd
1
2
3
4

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 28, 2008

Related Tutorials

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
bamiday
October 27, 2011
need help with ArrayList

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); } } }
DMCA.com