Total members 11890 |It is currently Thu Apr 18, 2024 3:40 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Cloning array list and its elements in java
Code:
import java.awt.Color;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test {

    public static void main(String[] args) {
        
        ArrayList catsArray1 
= new ArrayList();
        int size = 3;

            catsArray1.add(new Cat(13,5,Color.BLACK));
            catsArray1.add(new Cat(10,2,Color.WHITE));
            catsArray1.add(new Cat(12,4,Color.DARK_GRAY));

        

         
// Clone array list elements
        ArrayList catsArray2 = (ArrayList) catsArray1.clone();
         for (int i = 0; i < size; i++) {
            try {
                catsArray2.set(i, ((Cat) catsArray1.get(i)).clone());
            } catch (CloneNotSupportedException ex) {
                  Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
           }
          }


     

    
}
}
 

Code:
class Cat implements Cloneable {

    private float weight;
    private float age;
    private Color color;

    public Cat(float weight, float age, Color color) {
           this.weight=weight;
           this.age=age;
           this.color=color;
    }

    public float getAge() {
        return age;
    }

    public void setAge(float age) {
        this.age = age;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }

  
    public String toString
() {
        return "Cat Details"+color+" "+age+" "+weight;
    }

    @Override
    public Object clone
() throws CloneNotSupportedException {
        return new Cat( weight,  age,  color);
    }
}
 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : clone arrayList elements
 when to use clone in java     -  
 how to make ArrayList ThreadSafe     -  
 A clone instance of class in php     -  
 Difference in order between HashSet and ArrayList     -  
 Add elements to the end of an array     -  
 elements of a GridBagLayout organized     -  
 count elements in a vector     -  
 Elements of a Java Program     -  
 lesson5: XSD Complex elements     -  
 Hide elements by class selector     -  



Topic Tags

Java Arrays
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com