lundi 14 avril 2014

tableaux - construction d'un vecteur de Java à partir de zéro - Stack Overflow


Long story short, I am in a university Comp Sci class, and the Professor wants us to build a Vector but from scratch. Are their any good tutorials on the web for something like this? I did a Google search but nothing really came up.


EDIT


According to the document the teacher gave out the vector needs to be able to do the following:



  • append(Object element) – appending the element at the end of the vector

  • clear() – make the vector collection empty

  • contains(Object element) – check whether the vector contains the element

  • elementAt(int index) – access the element at the given index

  • indexOf(Object element) – find the index of the element

  • insertAt(int index, Object element) – insert the element at the given index

  • isEmpty() – check whether the vector is empty

  • removeAt(int index) – remove the element at the given index

  • remove(Object element) – remove the element from the vector

  • replace(int index, Object element) – replace the element at the given index with the given element

  • size() – get the number of elements in the current vector

  • ensureCapacity(int minCapacity) – make sure the vector gets at least the given capacity

  • clone() – returns a cloned copy of this vector

  • removeRange(int fromIndex, int toIndex) – removes from this vector all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive

  • toString() – returns a string representation of this vector, containing the String representation of each element

  • reverse() – reverse the elements in this vector

  • merge(MyVector vector2) – add all the elements in vector2 to the end of this vector


Additionally the class needs to implment Cloneable and be self expanding.


This is what I have come up with so far:


package collection;

public class MyVector implements Cloneable {

protected Object[] items;
protected int arraySize;
protected int maxCap;

public MyVector (int initialCapacity) {
super();
if (initialCapacity < 0) {
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
}
this.items = new Object[initialCapacity];
this.arraySize = 0;
this.maxCap = initialCapacity;
}

public MyVector() {
this(10);
}

public void append(Object element) {
int newArraySize = this.arraySize + 1;
if(this.maxCap == newArraySize) {
this.items = this.increaseCap(newArraySize);
this.items[this.arraySize] = element;
this.arraySize += 1;
//System.out.println(this.items[this.arraySize);
} else {
this.items[this.arraySize] = element;
this.arraySize +=1;
}
}
@Override
public String toString() {
String output = "[";
//output = this.items[0].toString();
for(int i = 0; i < this.arraySize; i++) {
output += this.items[i] + ", ";
}
output += "]";
return output;
}
public void clear() {
for(int i = 0; i < this.arraySize; i++) {
this.items[i] = null;
this.arraySize = 0;
}
}
public boolean contains(Object element) {
boolean doesContain = false;
for(int i = 0; i < this.arraySize; i++) {
if(element == this.items[i]) {
doesContain = true;
i = this.arraySize;
}
}
return doesContain;
}
public Object elementAt(int index) {
if(this.arraySize >= index) {
return this.items[index];
} else {
Object temp = null;
System.out.println("No index of " + index);
return temp;
}
}
public Object indexOf(Object element) {
Object index = "No value found";
for(int i = 0; i < this.arraySize; i++) {
if(element == this.items[i]) {
index = i;
break;
}
}
return index;
}
public boolean isEmpty() {
if(this.arraySize == 0) {
return true;
}
return false;
}
public void replace(int index, Object element) {
if(this.arraySize > index) {
this.items[index] = element;
} else {
System.out.println("No element at " + index);
}
}
public int size() {
return this.arraySize;
}
public void reverse() {
Object[] temp = new Object[this.items.length];
int j = this.arraySize;
for(int i = 0; i < this.arraySize; i++) {
temp[j] = this.items[i];
j--;
}
this.items = temp;
}
public void ensureCapacity(int minCapacity) {
if(minCapacity > this.items.length) {
this.items = this.increaseCap(minCapacity);
}
}
public Object[] increaseCap(int minCap) {
Object[] arr1 = new Object[minCap * 2];
for(int i = 0; i < minCap; i++) {
arr1[i] = this.items[i];
}
this.maxCap = this.maxCap * 2;
return arr1;
}
@Override
public Object clone() {
return this.items;
}
public boolean checkIndex(int index) {
boolean check = false;
if(index < this.arraySize) {
check = true;
}
return check;
}
public void removeAt(int index) {
if(true == this.checkIndex(index)) {
Object[] temp = new Object[this.arraySize - 1];
for(int j = 0; j < index; j++) {
temp[j] = this.items[j];
}
for(int j = index + 1; j < this.arraySize; j++) {
temp[j-1] = this.items[j];
}
this.items = temp;
this.arraySize = this.arraySize - 1;
}
}
public void insertAt(int index, Object element) {
if (this.checkIndex(index) == true) {
Object[] temp = new Object[this.arraySize];
for(int i = index; i < this.arraySize; i++) {
temp[i+1] = this.items[i];
}
this.items[index] = element;
for (int i = index + 1; i < this.arraySize; i++) {
this.items[i] = temp[i - 1];
}
this.arraySize = this.arraySize - 1;
}
}
public void remove(Object element) {
for(int i = 0; i < this.items.length; i++) {
if(this.items[i] == element) {
this.removeAt(i);
}
}
}
public void removeRange(int fromIndex, int toIndex) {
for(int i = fromIndex; i < toIndex; i++) {
this.removeAt(i);
}
}
public void merge(MyVector vector2) {
this.ensureCapacity(vector2.size() + this.arraySize);
for(int i = 0; i < vector2.size(); i++) {
this.append(vector2);
}
}
}



Assuming your assignment is replicating java.util.Vector, I would look at what a Vector is in order to replicate it:



Vector implements a dynamic array. It is similar to ArrayList, but with two differences:



  1. Vector is synchronized.


  2. Vector contains many legacy methods that are not part of the collections framework.




You could attempt to use an ArrayList in a synchronous manner in order to replicate a Vector, but I'm sure there are much better solutions.



Long story short, I am in a university Comp Sci class, and the Professor wants us to build a Vector but from scratch. Are their any good tutorials on the web for something like this? I did a Google search but nothing really came up.


EDIT


According to the document the teacher gave out the vector needs to be able to do the following:



  • append(Object element) – appending the element at the end of the vector

  • clear() – make the vector collection empty

  • contains(Object element) – check whether the vector contains the element

  • elementAt(int index) – access the element at the given index

  • indexOf(Object element) – find the index of the element

  • insertAt(int index, Object element) – insert the element at the given index

  • isEmpty() – check whether the vector is empty

  • removeAt(int index) – remove the element at the given index

  • remove(Object element) – remove the element from the vector

  • replace(int index, Object element) – replace the element at the given index with the given element

  • size() – get the number of elements in the current vector

  • ensureCapacity(int minCapacity) – make sure the vector gets at least the given capacity

  • clone() – returns a cloned copy of this vector

  • removeRange(int fromIndex, int toIndex) – removes from this vector all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive

  • toString() – returns a string representation of this vector, containing the String representation of each element

  • reverse() – reverse the elements in this vector

  • merge(MyVector vector2) – add all the elements in vector2 to the end of this vector


Additionally the class needs to implment Cloneable and be self expanding.


This is what I have come up with so far:


package collection;

public class MyVector implements Cloneable {

protected Object[] items;
protected int arraySize;
protected int maxCap;

public MyVector (int initialCapacity) {
super();
if (initialCapacity < 0) {
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
}
this.items = new Object[initialCapacity];
this.arraySize = 0;
this.maxCap = initialCapacity;
}

public MyVector() {
this(10);
}

public void append(Object element) {
int newArraySize = this.arraySize + 1;
if(this.maxCap == newArraySize) {
this.items = this.increaseCap(newArraySize);
this.items[this.arraySize] = element;
this.arraySize += 1;
//System.out.println(this.items[this.arraySize);
} else {
this.items[this.arraySize] = element;
this.arraySize +=1;
}
}
@Override
public String toString() {
String output = "[";
//output = this.items[0].toString();
for(int i = 0; i < this.arraySize; i++) {
output += this.items[i] + ", ";
}
output += "]";
return output;
}
public void clear() {
for(int i = 0; i < this.arraySize; i++) {
this.items[i] = null;
this.arraySize = 0;
}
}
public boolean contains(Object element) {
boolean doesContain = false;
for(int i = 0; i < this.arraySize; i++) {
if(element == this.items[i]) {
doesContain = true;
i = this.arraySize;
}
}
return doesContain;
}
public Object elementAt(int index) {
if(this.arraySize >= index) {
return this.items[index];
} else {
Object temp = null;
System.out.println("No index of " + index);
return temp;
}
}
public Object indexOf(Object element) {
Object index = "No value found";
for(int i = 0; i < this.arraySize; i++) {
if(element == this.items[i]) {
index = i;
break;
}
}
return index;
}
public boolean isEmpty() {
if(this.arraySize == 0) {
return true;
}
return false;
}
public void replace(int index, Object element) {
if(this.arraySize > index) {
this.items[index] = element;
} else {
System.out.println("No element at " + index);
}
}
public int size() {
return this.arraySize;
}
public void reverse() {
Object[] temp = new Object[this.items.length];
int j = this.arraySize;
for(int i = 0; i < this.arraySize; i++) {
temp[j] = this.items[i];
j--;
}
this.items = temp;
}
public void ensureCapacity(int minCapacity) {
if(minCapacity > this.items.length) {
this.items = this.increaseCap(minCapacity);
}
}
public Object[] increaseCap(int minCap) {
Object[] arr1 = new Object[minCap * 2];
for(int i = 0; i < minCap; i++) {
arr1[i] = this.items[i];
}
this.maxCap = this.maxCap * 2;
return arr1;
}
@Override
public Object clone() {
return this.items;
}
public boolean checkIndex(int index) {
boolean check = false;
if(index < this.arraySize) {
check = true;
}
return check;
}
public void removeAt(int index) {
if(true == this.checkIndex(index)) {
Object[] temp = new Object[this.arraySize - 1];
for(int j = 0; j < index; j++) {
temp[j] = this.items[j];
}
for(int j = index + 1; j < this.arraySize; j++) {
temp[j-1] = this.items[j];
}
this.items = temp;
this.arraySize = this.arraySize - 1;
}
}
public void insertAt(int index, Object element) {
if (this.checkIndex(index) == true) {
Object[] temp = new Object[this.arraySize];
for(int i = index; i < this.arraySize; i++) {
temp[i+1] = this.items[i];
}
this.items[index] = element;
for (int i = index + 1; i < this.arraySize; i++) {
this.items[i] = temp[i - 1];
}
this.arraySize = this.arraySize - 1;
}
}
public void remove(Object element) {
for(int i = 0; i < this.items.length; i++) {
if(this.items[i] == element) {
this.removeAt(i);
}
}
}
public void removeRange(int fromIndex, int toIndex) {
for(int i = fromIndex; i < toIndex; i++) {
this.removeAt(i);
}
}
public void merge(MyVector vector2) {
this.ensureCapacity(vector2.size() + this.arraySize);
for(int i = 0; i < vector2.size(); i++) {
this.append(vector2);
}
}
}


Assuming your assignment is replicating java.util.Vector, I would look at what a Vector is in order to replicate it:



Vector implements a dynamic array. It is similar to ArrayList, but with two differences:



  1. Vector is synchronized.


  2. Vector contains many legacy methods that are not part of the collections framework.




You could attempt to use an ArrayList in a synchronous manner in order to replicate a Vector, but I'm sure there are much better solutions.


1 commentaire:

  1. I'm attempting to compose a program that will naturally make and refresh a rundown of items on Amazon. So what I'm doing is sendin...
    DTI

    RépondreSupprimer