『壹』 java中vector是什麼有什麼作用

翻譯過來就是向量,數據結構了類似於List,區別在於Vector是線程安全的,所以效率低於List。

『貳』 java中的vector 是什麼意思呢

java中vector和數據非常類似,兩者之間也經常成對出現,下面是兩者的比較:

1、數組:java arrays的元素個數不能下標越界,從很大程度上保證了java程序的安全性,但創建時必須指定數組的大小,並不能再改變。

2、vector:對比於array,當更多的元素被加入進來以至超出其容量時,vector的size會動態增長,而array容量是定死的。同時,vector在刪除一些元素後,其所有下標大於被刪除元素的元素都依次前移,並獲得新下標比原來的小了)。

『叄』 Java中Vector的定義

Vector 可實現自動增長的對象數組。
java.util.vector提供了向量類(vector)以實現類似動態數組的功能。在Java語言中沒有指針的概念,但如果正確靈活地使用指針又確實可以大大提高程序的質量。比如在c,c++中所謂的「動態數組」一般都由指針來實現。為了彌補這個缺點,Java提供了豐富的類庫來方便編程者使用,vector類便是其中之一。事實上,靈活使用數組也可以完成向量類的功能,但向量類中提供大量的方法大大方便了用戶的使用。
創建了一個向量類的對象後,可以往其中隨意插入不同類的對象,即不需顧及類型也不需預先選定向量的容量,並可以方便地進行查找。對於預先不知或者不願預先定義數組大小,並且需要頻繁地進行查找,插入,刪除工作的情況。可以考慮使用向量類。

向量類提供了三種構造方法:
public vector()
public vector(int initialcapacity,int capacityIncrement)
public vector(int initialcapacity)
舉例說明:
import java.util.Vector;
import java.lang.*;
import java.util.Enumeration;
public class VectorApp
{
public static void main(String args[])
{
Vector v1 = new Vector();
Integer integer1= new Integer(1);
//加入為字元串對象
v1.addElement("one");
//加入的為integer的對象
v1.addElement(integer1);
v1.addElement(integer1);
v1.addElement("two");
v1.addElement(new Integer(2));
v1.addElement(integer1);
v1.addElement(integer1);
//轉為字元串並列印
System.out.println("The Vector v1 is:\n\t"+v1);
//向指定位置插入新對象
v1.insertElement("three",2);
v1.insertElement(new Float(3.9),3);
System.out.println("The Vector v1(used method
insertElementAt()is:\n\t)"+v1);
//將指定位置的對象設置為新的對象
//指定位置後的對象依次往後順延
v1.setElementAt("four",2);
System.out.println("The vector v1 cused method setElmentAt()is:\n\t"+v1);
v1.removeElement(integer1);
//從向量對象v1中刪除對象integer1
//由於存在多個integer1,所以從頭開始。
//找刪除找到的第一個integer1.
Enumeration enum = v1.elements();
System.out.println("The vector v1 (used method removeElememt()is");
while(enum.hasMoreElements())
System.out.println(enum.nextElement()+"");
System.out.println();
//使用枚舉類(Enumeration)的方法取得向量對象的每個元素。
System.out.println("The position of Object1(top-to-botton):"+v1.indexOf(integer1));
System.out.println("The position of Object1(tottom-to-top):"+v1.lastIndexOf(integer1));
//按不同的方向查找對象integer1所處的位置
v1.setSize(4);
System.out.println("The new Vector(resized the vector)is:"+v1);
//重新設置v1的大小,多餘的元素被拋棄
}
}
運行結果:
運行結果:
E:\java01>java VectorApp
The vector v1 is:[one,1,1,two,2,1,1]
The vector v1(used method insetElementAt()) is:
[one,1,three,3.9,1,two,2,1,1]
The vector v1(used method setElementAt()) is:
[one,1,four,3.9,1,two,2,1,1]
The vector v1(useed method removeElement()) is:
one four 3.9 1 two 2 1 1
The position of object1(top-to-botton):3
The position of object1(botton-to-top):7
The new Vector(resized the vector) is:
[one,four,3.9,1]

『肆』 java中vector是什麼

importjava.util.Enumeration;
importjava.util.Vector;

/*
*Vector的特有功能:
*1:添加功能
* publicvoidaddElement(Objectobj) -- add()
*2:獲取功能
* publicObjectelementAt(intindex) --get()
* publicEnumerationelements() -- Iteratoriterator()
* booleanhasMoreElements() hasNext()
* ObjectnextElement() next()
*
*JDK升級的原因:
* A:安全
* B:效率
* C:簡化書寫
*/
publicclassVectorDemo{
publicstaticvoidmain(String[]args){
//創建集合對象
Vectorv=newVector();

//添加功能
v.addElement("hello");
v.addElement("world");
v.addElement("java");

//遍歷
for(intx=0;x<v.size();x++){
Strings=(String)v.elementAt(x);
System.out.println(s);
}

System.out.println("------------------");

Enumerationen=v.elements();//返回的是實現類的對象
while(en.hasMoreElements()){
Strings=(String)en.nextElement();
System.out.println(s);
}
}
}

『伍』 java中Vector的使用

import java.util.Vector;

public class Test {

public static void main(String[] args) {

Vector<Student> vector = new Vector<Student>();

Student stu1 = new Student("Zhang San", "001", 1);
Student stu2 = new Student("Li Si", "003", 2);
Student stu3 = new Student("Wang wu", "007", 3);

vector.add(stu1);
vector.add(stu2);
vector.add(stu3);

for(Student stu: vector){
System.out.println(stu.toString());
}

}
}

class Student{
private String name;
private String sno;
private int grade;

public Student(String name, String sno, int grade){
this.name = name;
this.sno = sno;
this.grade = grade;
}

public String toString(){
return name +": " + name + ", sno: " + sno +", Grade: " + grade;
}
}
---------------------------
Zhang San: Zhang San, sno: 001, Grade: 1
Li Si: Li Si, sno: 003, Grade: 2
Wang wu: Wang wu, sno: 007, Grade: 3

列表把vectore裡面的對象放到列表中,插入值需要vectore.add(object);,修改:根據索引去更新student對象;刪除:從vectore裡面刪除;瀏覽:迭代取vectore; 統計人數:返回vectore。size()

『陸』 java中Vector的用法是什麼

向量 跟數組很相似 不過功能比數組強大 創建方法和數組一樣 用法的話就比數組好用多了 具體的使用方法網上有很多你去好好看看吧

『柒』 java中vector是

Vector 類 提 供 了 實 現 可 增 長 數 組 的 功 能, 隨 著 更 多 元 素 加 入 其 中, 數 組 變 的 更 大。 在 刪 除 一 些 元 素 之 後, 數 組 變 小。

Vector 有 三 個 構 造 函 數,

public Vector(int initialCapacity,int capacityIncrement)

public Vector(int initialCapacity)

public Vector()

---- Vector 運 行 時 創 建 一 個 初 始 的 存 儲 容 量initialCapacity, 存 儲 容 量 是 以capacityIncrement 變 量 定 義 的 增 量 增 長。 初 始 的 存 儲 容 量 和capacityIncrement 可 以 在Vector 的 構 造 函 數 中 定 義。 第 二 個 構 造 函 數 只 創 建 初 始 存 儲 容 量。 第 三 個 構 造 函 數 既 不 指 定 初 始 的 存 儲 容 量 也 不 指 定capacityIncrement。

---- Vector 類 提 供 的 訪 問 方 法 支 持 類 似 數 組 運 算 和 與Vector 大 小 相 關 的 運 算。 類 似 數 組 的 運 算 允 許 向 量 中 增 加, 刪 除 和 插 入 元 素。 它 們 也 允 許 測 試 矢 量 的 內 容 和 檢 索 指 定 的 元 素, 與 大 小 相 關 的 運 算 允 許 判 定 字 節 大 小 和 矢 量 中 元 素 不 數 目。

---- 現 針 對 經 常 用 到 的 對 向 量 增, 刪, 插 功 能 舉 例 描 述:

addElement(Object obj)

---- 把 組 件 加 到 向 量 尾 部, 同 時 大 小 加1, 向 量 容 量 比 以 前 大1

insertElementAt(Object obj, int index)

---- 把 組 件 加 到 所 定 索 引 處, 此 後 的 內 容 向 後 移 動1 個 單 位

setElementAt(Object obj, int index)

---- 把 組 件 加 到 所 定 索 引 處, 此 處 的 內 容 被 代 替。

---- removeElement(Object obj) 把 向 量 中 含 有 本 組 件 內 容 移 走。

---- removeAllElements() 把 向 量 中 所 有 組 件 移 走, 向 量 大 小 為0。

---- 例 如:

import java.lang.System;

import java.util.Vector;

import java.util.Emumeration;

public class Avector{

public static void main(String args[])

{

0. Vector v=new Vector();

1. v.addElement("one");

2. addElement("two");

3. v.addElement("three");

4. v.insertElementAt("zero",0);

5. v.insertElementAt("oop",3);

6. v.setElementAt("three",3);

7. v.setElementAt("four",4);

8. v.removeAllElements();

}

}

Vector中的變化情況:

1. one 2. one 3. one 4. zero 5.zero 6. zero 7. zero

8.

two two one one one one

three two two two two

three oop three three

three three four

---- 另 外,Vector 在 參 數 傳 遞 中 發 揮 著 舉 足 輕 重 的 作 用。

---- 在Applet 中 有 一 塊 畫 布(Canvas) 和 一 個(Panel), 而Panel 中 放 著 用 戶 要 輸 入 的 信 息, 根 據 這 些 信 息 把 參 數 傳 遞 到canvas 中, 這 時 在Java 中 用 一 個 接 口(Interface), 而 在 接 口 中 需 用 一 個Vector 去 傳 遞 這 些 參 數。 另 外, 在 一 個 類 向 另 一 個 類 參 數 傳 遞 就 可 以 用 這 種 方 法。

---- 例 如:

import java.util.Vector

interface codeselect{

Vector codeselect=new Vector();

}

顯示數學信息

Vector(0)存入學生編號

Vector(1)存入學科

---- 在Panel 中 當 用 戶 在 TextField 和Choice 中 選 擇 自 己 所 要 求 的 內 容, 程 序 中

---- 通 過 事 件 響 應 把 值 傳 到 向 量Vector 中。

---- 假 若 在Panel 類 中:

public void codepanel extends Panel{

public void init()

{

**.

TextField s=new TextField();

Choice c=new Choice();

c. addItem("語文");

c.addItem("數學");

c.addItem("政治");

add(s);

add (c);

**

}

public boolean handleEvent(Event event){

if(event.id==Event.ACTION_EVENT){

if(event.target.instanceof Textfield)

{

coderesult.setElementAt(s.getText(),0);

}

else if(event.target intanceof Choice)

{

coderesult.setElementAt(new Integer(c.getSelectedIndex()),1);

}

}

}

}

---- 這 時, 向 量 中 已 經 存 入 學 生 編 號 和 學 科 索 引 號(0 為 語 文,1 為 數 學,2 為 政 治)。

---- 而 在Canvas 中 得 到 此 值,

public class codecanvas extends Canvas{

public void code{

}

public void paint{

String str;

int t;

str=(String)coderesult.elementAt(0);

t=(new Integer(codeselect.elementAt(1).toString())).intValue();

if(t==0)

{

顯示語文信息

}

else if(t==1)

{

顯示數學信息

}

else if(t==2)

{

顯示政治信息

}

}

}

『捌』 Java中vector的用法

importjava.util.Scanner;
importjava.util.Vector;

publicclassTest{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Scannerinput=newScanner(System.in);
System.out.print("請輸入總人數:");
intp=input.nextInt();
/****初始化人員***/
Vector<Integer>v=newVector<Integer>();//初始化人員並進行編號
for(inti=1;i<=p;i++){
v.add(newInteger(i));
System.out.print(i+"");
}

/****報號***/
intnum=0;
while(v.size()>1){
for(inti=0;i<v.size();i++){
num++;
if((num%3)==0){
v.remove(i);
i--;
}
}
}
/*****結果*****/
for(inti=0;i<v.size();i++){
System.out.println(" 最後剩下的數是:"+v.get(i));
}
}
}

『玖』 java中Vector的用法

final class A
{
public Vector<Integer> intint = new Vector<Integer>(); //放整型數據容器
public Vector<String> stringstring = new Vector<String>(); //放字元串容器
public Vector<Boolean> boolbool = new Vector<Boolean>(); //放布爾型數據容器
}

public class List_test
{
public static A a = new A ();

public static void main(String[] args)
{
a.intint .add(1);
}
}

『拾』 java中vector的用法

這問題跟vector就沒什麼關系
在class裡面調用什麼對象的方法,我只能說都毫無意義~!
根本就執行不了,main函數能調用到么?
你在浪費思想...