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

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /