Programming Tutorials

Java program for changeable wrapper class

By: Issac in Java Tutorials on 2009-09-14  

In this tutorial we are going to see a changeable wrapper class example. In this example We have file named MainClass.java.. Here we want arraylist, list. So we have to import util package.We can use import java.util.ArrayList; , import java.util.List; in this example..

import java.util.ArrayList;
import java.util.List;

class Values {
  private int s;

  public Values(int x) 
 {
    s = x;
  }

  public int getNumber() 
 {
    return s;
  }

  public void setNumber(int s) 
  {
    this.s = s;
  }

  public void increment() 
  {
    s++;
  }

  public String toString()
 {
    return Integer.toString(s);
  }
}

public class MainClass
 {

  public static void main(String[] args)
 {
    List list = new ArrayList();
    for (int i = 0; i < 10; i++)
      list.add(new Values(i));
    System.out.println(list);
    for (int i = 0; i < list.size(); i++)
      ((Values) list.get(i)).increment();
    System.out.println(list);

  }
}

Output:

[0,1,2,3,4,5,6,7,8,9]
[1,2,3,4,5,6,7,8,9,10]






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)