Ciao a tutti,
ho questa interfaccia Vector<T>:
ora devo:codice:package util; public interface Vector<T> extends Iterable<T>{ public int size(); public int indexOf(T elem); public boolean contains(T elem); public T get(int indice); public T set(int indice, T elem); public void add(T elem); public void add(int indice, T elem); public void remove(T elem); public T remove(int indice); public void clear(); public boolean isEmpty(); public Vector<T> subVector(int da, int a); public void retainAll(Vector<T> v); public void removeAll(Vector<T> v); }
Scrivere una classe VectorAstratto<T> che implementi Vector<T> iterabile e concretizzi quanti più metodi possibili. Adattare la classe ArrayVector<T> già pronta come erede di VectorAstratto<T>. Sviluppare una nuova classe LinkedVector<T> erede di VectorAstratto<T> che implementa il concetto di vettore mediante una LinkedList.
mi chiarite un po le idee?
in che senso concretizzare i metodi? nel senso, il metodo size() lo concretizzo come?
si intende questo???codice:public int size(){ int c=0; for(int i=0; i<size()[???]; i++) c++; return c;
poi adattare ArrayVector<T> che invece di implementare, estende una astratta?
e poi cosa si intende per esmpreimere concetto di vettore mediande linkedlist?codice:package util;import java.util.*; public class ArrayVector<T> implements Vector<T>{ private int size; private T[]array; public ArrayVector(int capacita){ if(capacita<=0) throw new IllegalArgumentException(); array=(T[])new Object[capacita]; size=0; } public ArrayVector(){this(20);} //capacita di default public int size(){return size;} public int indexOf(T elem){ for(int i=0; i<size; i++) if(array[i].equals(elem)) return i; return -1; } public boolean contains(T elem){return indexOf(elem)!=-1;} public T get(int indice){ if(indice<0 || indice>=size) throw new IndexOutOfBoundsException(); return array[indice]; } public T set(int indice, T elem){ if(indice<0 || indice>=size) throw new IndexOutOfBoundsException(); T x=array[indice]; array[indice]=elem; return x; } public void add(T elem){ if(size==array.length) array=Arrays.copyOf(array,2*array.length); array[size]=elem; size++; } public void add(int indice, T elem){ if(indice<0 || indice<size) throw new IndexOutOfBoundsException(); if(size==array.length) array=Arrays.copyOf(array,2*array.length); for (int i=size-1; i>=indice; i--) array[i+1]=array[i]; array[indice]=elem; size++; } public void remove(T elem){ int i=indexOf(elem); if(i==-1) return; remove(i); } public T remove(int indice){ if(indice<0 || indice>=size) throw new IndexOutOfBoundsException(); T old=array[indice]; for(int i=indice+1; i<size; i++) array[i+1]=array[i]; size--; array[size]=null; if(size<array.length/2) array=Arrays.copyOf(array, array.length/2); return old; } public void clear(){ for(int i=0; i<size; i++) array[i]=null; size=0; } public boolean isEmpty(){ return size==0; } public Vector<T> subVector(int da, int a){ if(da<0 || a<0 || da>=size || a>size || da>=a) throw new RuntimeException(); Vector<T> v=new ArrayVector<T>(a-da); for(int j=da; j<a; j++) v.add(array[j]); return v; } public void retainAll(Vector<T> v){ for(int i=0; i<size(); i++) if(!array[i].equals(v)) this.remove(i); } //check it public void removeAll(Vector<T> v){ for(int i=0; i<size(); i++) if(array[i].equals(v)) this.remove(i); } public boolean equals(Object x){ if(!(x instanceof Vector)) return false; if(x==this) return true; Vector v=(Vector)x; if(this.size!=v.size()) return false; for(int i=0; i<this.size; i++) if(!array[i].equals(v.get(i))) return false; return true; } public String toString(){ StringBuilder sb=new StringBuilder(20); sb.append('['); for(int i=0; i<size; i++){ sb.append(array[i]); if(i<size-1) sb.append(","); } sb.append(']'); return sb.toString(); } public int hashCode(){ final int MOLT=41; int h=0; for(int i=0; i<size; i++) h=h*MOLT+array[i].hashCode(); return h; } public Iterator<T> iterator(){ return new Iteratore(); } private class Iteratore implements Iterator<T>{ //inner class private int corrente=-1; private boolean rimovibile=false; public boolean hasNext(){ if(corrente==-1) return size>0; return corrente<size-1; } public T next(){ if(!hasNext()) throw new NoSuchElementException(); corrente++; rimovibile=true; return array[corrente]; } public void remove(){ if(!rimovibile) throw new IllegalStateException(); rimovibile=false; ArrayVector.this.remove(corrente); corrente--; } } public static void main(String[]args){//demo Vector<Integer> v=new ArrayVector<Integer>(); //capacità default for(int i=0;i>10; i++) v.add(i); System.out.println(v); Vector<Integer> sv=v.subVector(4,10); System.out.println(sv); Vector<String> w=new ArrayVector(); Scanner sc=new Scanner(System.in); for(;;){ System.out.println("String(solo INVIO per terminare): "); String s=sc.nextLine(); if(s.length()==0)break; boolean flag=false; int indice=0; while(indice<w.size() && !flag){ String str=(String)w.get(indice); if(str.compareTo(s)>=0) flag=true; else indice++; } w.add(indice, s); } System.out.println(w); } }
scusate ragazzi, esame a breve![]()

Rispondi quotando