import java.util.*;
public class VectorDemo {
public static void main(String[] args){
Vector v = new Vector(4);
v.add("Test0");
v.add("Test1");
v.add("Test0");
v.add("Test2");
v.add("Test2");
v.remove("Test0"); //刪除指定內容的元素
v.remove(0); //按照索引號刪除元素
//獲得Vector中已有元素的個數
int size = v.size();
System.out.println("size:" + size);
//遍歷Vector中的元素
for(int i = 0;i < v.size();i++)
System.out.println(v.get(i));
}
}
當使用v.add(Object)時
其作用方式類似指標
假設我有一個class svm_node{
int index;
double value;
}
多個node欲放入vector中
必須用不同的node作為代表,不可更改同一個node放進去
會全部指向最後一個node
即"Vector"堆疊的是每個Object的記憶體位置
而不是複製一份堆疊進去
Vector vx = new Vector();
svm_node[] x = new svm_node[3]; x[0] = new svm_node(); x[0].index = color_r; x[0].value = 15.0; x[1] = new svm_node(); x[1].index = color_g; x[1].value = 163.0; x[2] = new svm_node(); x[2].index = color_b; x[2].value = 15.0; vx.add(x); //儲存SVM node的陣列svm_node[] y = new svm_node[3]; y[0] = new svm_node(); y[0].index = color_r; y[0].value = 255.0; y[1] = new svm_node(); y[1].index = color_g; y[1].value = 42.0; y[2] = new svm_node(); y[2].index = color_b; y[2].value = 0.0; vx.add(y); svm_node[] z = new svm_node[3]; z[0] = new svm_node(); z[0].index = color_r; z[0].value = 255.0; z[1] = new svm_node(); z[1].index = color_g; z[1].value = 255.0; z[2] = new svm_node(); z[2].index = color_b; z[2].value = 255.0; vx.add(z); _prob.x[0] = (svm_node[]) vx.get(0); _prob.x[1] = (svm_node[]) vx.get(1);
沒有留言:
張貼留言