Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.AbstractList;
- import java.util.ArrayList;
- import java.util.List;
- public class OnlyNList extends AbstractList<String> {
- private List<String> decorated ;
- private int n;
- public String get(int i) {
- return decorated.get(i);
- }
- public int size() {
- return decorated.size();
- }
- public OnlyNList(int n, List<String> decorated) {
- this.n = n;
- this.decorated = decorated;
- }
- public String set(int i,String e) {
- if (e.length() > this.n)
- throw new UnsupportedOperationException();
- return this.decorated.set(i,e);
- }
- public void add(int i,String e) {
- if (e.length() > this.n)
- throw new UnsupportedOperationException();
- this.decorated.add(i,e);
- }
- public String remove(int i) {
- return this.decorated.remove(i);
- }
- public static void main(String argv[]) {
- OnlyNList l = new OnlyNList(4, new ArrayList<String>());
- l.add("1234");
- System.out.println(l);
- try {
- l.add("12345");
- } catch (Exception e) {
- System.out.println("OK");
- }
- l.add("abcd");
- l.add("1ab2");
- l.add("xyz0");
- System.out.println(l);
- for(String s: l) System.out.println(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement