android 中vector reserve的用法的用法 坑 怎么替代,关于这几方面的一些看法

关于android中矢量图如何用,有坑,爬坑,如何替代的另一些看法 - 简书
下载简书移动应用
写了13668字,被75人关注,获得了267个喜欢
关于android中矢量图如何用,有坑,爬坑,如何替代的另一些看法
在安卓的发展历程中,由于设备碎片化的原故,谷歌在app中图标的适配上做出一步又一步的改进,大体有这么几个阶段:
首先有了drawable-(m|h|xh|xxh|xxxh)dpi
自android studio后,又有了mipmap-(m|h|xh|xxh|xxxh)dpi
随着android L的发布,带来了VectorDrawable,矢量图的支持
第一种方案大家都很熟悉, 但也是我们头痛的地方,因为每种icon都需要出几套不同分辨率,这无形的增加了app的容量,而且也增加了美工和开发人员的工作量,但是我们又不得不去做。第二种是第一种的升级版, 没有实质上的区别,但是在缩放上提供了更好的性能和更少的内存占用。第三种,矢量图,先大概解释下:矢量图在很久很久以前就已经应用起来了,是一种基于xml的图像,因为图片不提供具体的像素,只提供的是绘图的指令,所以好处是 占用内存非常小,性能高,可以任意缩放而不会失真,但是缺点也很明显,没有位图表达的色彩丰富。然而现在app风格越来越扁平, 拟物化已经成了过去,矢量图成了越来越多人的选择。但是,android和ios对于矢量图的支持还非常弱.
android在最新的支持包中,已经加入了向下兼容的库:VectorDrawableCompat和AnimatedDrawableCompat,关于这两点的介绍网上很多,但是大多相同,但是有坑。今天我就围绕这个上代码,让未上车的猿们抓紧时间上车。
首先,去哪找合适的矢量图:
找到你需要的图标,并下载svg
QQ图片28.png
在android中打开vector assert
4N`924]0H{31JI]I3IQ9CHH.png
点击Local SVG 选择路径,并命名drawable文件名字
QQ图片31.png
就在drawable目录生成了如下图
QQ图片13.png
ok, 一张可以任意缩放的图片有了。接着看怎么引用,先讲解一下, android L 以后矢量图是以vectorDrawable的形式来使用了, 但是这个库只支持L以后的,于是谷歌出了一个兼容包:VectorDrawableCompat,AnimatedVectorDrawableCompat
As you may have seen on the Support Lib 23.2.0, we now have compatible vector drawable implementations in the support libraries: VectorDrawableCompat and Animated VectorDrawableCompat.
意思是说 在appcompat 23.2.0开始,提供了以上两种支持库一个用于兼容矢量图,但是这个支持库要使用的话,还得在app的gradle里面加个这样的配置:
//在gradle2.0及以上:
defaultConfig {
vectorDrawables.useSupportLibrary = true
//在gradle 1.5以前
defaultConfig {
// Stops the Gradle plugin’s automatic rasterization of vectors
generatedDensities = []
// Flag to tell aapt to keep the attribute ids around
aaptOptions {
additionalParameters "--no-version-vectors"
ok , 配置完毕,下一步是怎么使用
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
xmlns:app="/apk/res-auto"
&android.support.v7.widget.AppCompatImageView
android:layout_width="42dp"
android:layout_height="42dp"
app:srcCompat="@drawable/icon_shopping"/&
&/LinearLayout&
QQ图片49.png
ok , 可以使用,试着改变下图标大小,会发现,完全一样,没有一点模糊 。这里要说到,这种的局限性:1.只能用于AppCompatImageView或者AppCompatImageButton或其子类,而且必须在app:srcCompat标签中,额,那我要用在TextView,Button上怎么办?我试试:
&android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:drawableLeft="@drawable/icon_shopping"
android:text="我想要小图标"/&
Caused by: android.content.res.Resources$NotFoundException:
File res/drawable/icon_shopping.xml from drawable resource ID #0x7f02004d
at android.content.res.Resources.loadDrawable(Resources.java:2101)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.TextView.&init&(TextView.java:806)
at android.support.v7.widget.AppCompatTextView.&init&(AppCompatTextView.java:60)
at android.support.v7.widget.AppCompatTextView.&init&(AppCompatTextView.java:56)
嗯,除了一大堆错误,没有别的效果, 怎么办?
You don’t have to use StateListDrawable. It also works with InsetDrawable, LayerDrawable, LevelListDrawable and RotateDrawable containers. The only rule is that the vector needs to be in a separate file.
在android官方推文中找到这句话 , 意味着,我们要在普通控件上使用Vector,就必须依附于StateListDrawable,InsetDrawable,LayerDrawable,LevelListDrawable,RotateDrawable,所以我来试试?
&?xml version="1.0" encoding="utf-8"?&
&selector xmlns:android="/apk/res/android"&
&item android:drawable="@drawable/icon_shopping"/&
&/selector&
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:drawableLeft="@drawable/shopping_res"
android:text="我想要小图标"/&
结果,相信有些哥们也尝试到这里了, 仍然是一大堆的错误。。。嗯,不是说包在那几个drawable里面就可以用了吗?忽悠人?这个帖子就此结束?不不不。
First up, this functionality was originally released in 23.2.0, but then we found some memory usage and Configuration updating issues so we it removed in 23.3.0. In 23.4.0(technically a fix release)we’ve re-added the same functionality but behind a flag which you need to manually enable.
我英文不太好, 应该是说,在23.2.0中放出了这个功能,但是后来发现了一些bug ,在23.3.0里面又移除了,后来在23.4.0中修复这些bug , 但是我们没有默认开启了,你需要通过一个标志来启用,呵呵哒。。这就是报错的原因了。。
If you want re-enable this, just stick this at the top of your Activity:
AppCompatDelegate.setCompatVectorFromSourcesEnabled(true);
嗯,这句话简单, 就是说,你要想再用上, 就在你的activity里面加上这句来启用vector ,结果怎样呢?
QQ图片49.png
ok ~问题解决。嗯,到了这一步,我们已经可以自由的使用矢量图标了呢,那怎么改变图标颜色呢?这个我要说声抱歉,我还没有想在xml里面设置图标背景的方法, 有人会说drawableTint,这个可以,但是只支持api23以上,嗯,所以:1.对于vector,我只想到了在xml里面改变fillColor来改变颜色,但这样就不好了,几个颜色就要几个图片,2.可以用代码在合适的位置设置:
VectorDrawableCompat a = VectorDrawableCompat.create(getResources(), R.drawable.icon_shopping, getTheme());
a.setTint(Color.RED); //设置单一的颜色
a.setTintList(ColorStateList.valueOf(Color.RED));//设置状态性的,比如点击一个颜色,未点击一个颜色
DrawableCompat.setTint(a,Color.RED); //用这个v4提供的也可,这个适用于任意的drawable着色
3.可以在AnimatedVectorDrawable中用动画来改变vector的颜色 ,嗯,第三个方案涉及的知识又多了起来, 这个可以再起一个博客详细来说,先略过。
嗯,说好了爬坑,就不可能到这里就结束了。
前面所说的这个矢量图只能用在AppcomatImageView,AppcompatImageButton,但实际上,用ImageView,ImageButton加上app:srcCompat也行, 为什么?因为你用了appcomat后,它会自动的把你的一部分控件转成AppCompatXxx的控件。
总结一下,这个矢量图的缺点吧:
1.麻烦, 需要下载-&vector asset转换-&用在非imageview中还要再写一个xml包裹起来才可用
2.不能随心所欲的在xml布局中任意切换图标颜色 , 如果在vector的xml里面改fillcolor,则这个图标就不好复用了。我的意思则是一处创建,随意使用。
3.支持库正在更新的过程中, 可能出现各种各样bug,也可能出现前面改动api实现而掉坑的情况。
那怎么办?怎么办?前面介绍的 :
对,就是它。后面要介绍的
它的原理是,把你想要的矢量图标打包成一个ttf,在android中应用这个ttf,就可以随心所欲了,怎么个随心所欲?用TextView的setText设置图标, setTextSize设置大小, 用TextColor设置图标颜色 ,只要能显示String的控件,都可以用,这样说来如何 ?
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
xmlns:app="/apk/res-auto"
&android.support.v7.widget.AppCompatImageView
android:layout_width="42dp"
android:layout_height="42dp"
app:srcCompat="@drawable/icon_shopping"/&
&android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:drawableLeft="@drawable/shopping_res"
android:text="我想要小图标"/&
&com.example.liufan.demo1.IconfontView
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:textSize="60sp"
android:textColor="#f00"
android:text="?"
android:layout_height="wrap_content" /&
&/LinearLayout&
Paste_Image.png
这个IconfontView,是继承自TextView,在初始化的时候加载了字体而已,& # xe725; 这个是上图图标的代码。嗯,这个并不算是什么高科技, 只是一个字体而已, 我就不长篇大论了, 下面贴出获取的流程吧,图从官网拿的:
Paste_Image.png
选中一堆需要的图标并加入购物车, 然后再这里点下载到本地,
Paste_Image.png
其中iconfont.ttf就是我们需要的字体了, 其他的那些在web上用的, 可以忽略。把iconfont.ttf复制到项目中assets中,然后,
public class IconfontView extends TextView {
public IconfontView(Context context) {
super(context);
initFont();
public IconfontView(Context context, AttributeSet attrs) {
super(context, attrs);
initFont();
public IconfontView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initFont();
private void initFont() {
final Typeface iconfont = Typeface.createFromAsset(getContext().getApplicationContext().getAssets(), "iconfont/iconfont.ttf");
setTypeface(iconfont );
这里就是替换一下字体而已,还有就是这个iconfont什么的, 这个我由于写的是demo就写死的, 这里可以自定义一个属性,用来指定具体要用的字体 。
ok , 现在打开解压目录的demo.html
Paste_Image.png
每个图标下的& #xe000就是这个图标的代码, 可以在IconfontView的text中设置了, 看下效果,
&com.example.liufan.demo1.IconfontView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textColor="#f00"
android:textSize="200sp" /&
&com.example.liufan.demo1.IconfontView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textColor="#f0f"
android:textSize="200sp" /&
Paste_Image.png
好了, 就是这些了, 基本全面, 如果帮忙您,希望您能帮我顶起来让更多人看到,如果说的有误,希望我能收到您的指正。
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
被以下专题收入,发现更多相似内容:
如果你是程序员,或者有一颗喜欢写程序的心,喜欢分享技术干货、项目经验、程序员日常囧事等等,欢迎投稿《程序员》专题。
专题主编:小...
· 163242人关注
玩转简书的第一步,从这个专题开始。
想上首页热门榜么?好内容想被更多人看到么?来投稿吧!如果被拒也不要灰心哦~入选文章会进一个队...
· 127301人关注
Android老鸟给新人的建议、资源。
更优质的原创内容,欢迎关注技术公众号,微信搜索:“Open软件开发小组”或者“open_dev”
· 11699人关注
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
选择支付方式:以下Drawables的功能帮助你在应用中实现Material Design:
图片资源着色
在android 5.0(api 21)和更高版本,可以着色bitmap和.9 png 通过定义透明度遮盖。你可以着色通过使用颜色资源或者主题的属性去解析颜色资源(比如,?android:attr/colorPrimary).通常我们创建一次,然后资源自适应主题。
你可以给BitmapDrawable或NinePatchDrawable对象着色使用setTint()方法。你可以可以在布局文件中使用android:tint和android:tintMode属性设置着色颜色和着色模式。
从图片中抽取高亮颜色
support library r21和更高的版本中包括了Palette类,可以从一个图片中提取高亮颜色。这个类可以提起以下几种突出颜色:
Vibrant 充满生机
Vibrant dark 暗的充满生机
Vibrant light 亮的充满生机
Muted 柔和
Muted dark 暗的柔和
Muted light 亮的柔和
传递一个Bitmap对象给静态方法Palette.generate(),它会在后台线程帮你从后台线程提取颜色。如果你不能使用这个后台线程,使用Palette.generateAsync()方法,并且设置一个监听器listener.
你可以从图片中取得突出颜色使用Palette类中的getter方法,比如Palette.getVibrantColor.
在项目中使用Palette方法,需要在项目中包含v7包palette的jar, gradle dependecy添加的方式是:
compile 'com.android.support:palette-v7:21.0.+'
下面这个是示例代码:
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
&&&&& public void onGenerated(Palette palette) {
&&&&&&&&& // Do something with colors...
&&&&&&&&& palette.getVibrantColor(Color.BLACK); //get a color in rgb value
更多信息,请查看Paltette的api文档:
创建矢量drawables
在android 5.0和更高版本中,可以创建矢量的drawable,在缩放的时候不会失真。你只需要定义一个矢量图片文件,相反的,使用bitmap位图则需要针对不同的分辨率创建多个文件。创建一个矢量图片,你需要说明图形的详细,在xml文件的 标签下。
下面是一个例子:
&? xml version = &1.0& encoding = &utf-8& ?&
&!-- res/drawable/heart.xml --&
&&&& xmlns:android = &/apk/res/android&
&&&& android:width = &256dp&
&&&& android:height = &256dp&
&&&& android:viewportHeight = &32&
&&&& android:viewportWidth = &32& &
&&&& &!-- draw a path --&
&&&& & path
&&&&&&&& android:fillColor = &#f15467&
&&&&&&&& android:pathData=&M20.5,9.5
&&&&&&&&&&&&&&&&&&&&&&&& c-1.955,0,-3.83,1.268,-4.5,3
&&&&&&&&&&&&&&&&&&&&&&&& c-0.67,-1.732,-2.547,-3,-4.5,-3
&&&&&&&&&&&&&&&&&&&&&&&& C8.957,9.5,7,11.432,7,14
&&&&&&&&&&&&&&&&&&&&&&&& c0,3.53,3.793,6.257,9,11.5
&&&&&&&&&&&&&&&&&&&&&&&& c5.207,-5.242,9,-7.97,9,-11.5
&&&&&&&&&&&&&&&&&&&&&&&& C25,11.432,23.043,9.5,20.5,9.5z&/&
&/ vector &
上面的图显示效果如下:
矢量图片在android表现为VectorDrawable对象。更多信息,查看Svg Path reference。17913人阅读

vector::erase():从指定容器删除指定位置的元素或某段范围内的元素
vector::erase()方法有两种重载形式
iterator erase(&& iterator _Where);
iterator erase(&& iterator _First,&& iterator _Last);
如果是删除指定位置的元素时:
返回值是一个迭代器,指向删除元素下一个元素;
如果是删除某范围内的元素时:返回值也表示一个迭代器,指向最后一个删除元素的下一个元素;
看下面的程序,目的是删除数组里面的所有值为6的元素:
#include&iostream&
#include&vector&
int main()
vector&int&
array.push_back(1);
array.push_back(6);
array.push_back(3);
array.push_back(6);
array.push_back(6);
array.push_back(2);
vector&int&::
vector&int&::iterator itor2;
for(itor=array.begin();itor!=array.end();)
if(6==*itor)
array.erase(itor2);
itor=array.begin();
for(itor=array.begin();itor!=array.end();)
cout&&(*itor++);
getchar();
运行结果输出1362,可见其中一个6并未删除,这是迭代器的问题。
原因在于erase以后,itor已经指向下一个元素了,不应该在itor++,否则会跳过下一个元素,即连续两个6时跳过了第二个6.
另外,在itor2=itor时,两个itor是一样的,这样做并无意义。可修改如下:
vector&int&::
// vector&int&::iterator itor2;
for(itor=array.begin();itor!=array.end();)
if(6==*itor)
array.erase(itor);
vector&int&::
for(itor=array.begin();itor!=array.end();itor++)
if(6==*itor)
array.erase(itor);
也可以使用remove方法:
array.earse( remove(array.begin(), array.end(),6),& array.end() );
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:160728次
积分:1954
积分:1954
排名:第14404名
原创:44篇
转载:29篇
评论:22条
(1)(1)(1)(2)(1)(1)(3)(1)(4)(4)(5)(8)(2)(4)(6)(3)(2)(1)(3)(7)(1)(2)(2)(1)(2)(1)(1)(1)(2)(1)(1)(1)(7)HDU5127 神坑题---vector 、 list 、 deque 的用法区别 - Gdragon(*^-^*) - 博客园
题意:三个操作
& & & & 1 &a b : 队列中加入(x = a, y = b);
& & & & -1 &a b : 队列中减去(x = a, y = b);
& & & & 0 &p q :从队列的数对中查询哪一对x,y能够让 p * x + q * y最大;
分析:因为一开始就觉得如果暴力绝对会超时,但是时限是30 000 ms,而且看见FB的又是8800ms过的,所以就暴力一次试试,但是TLE.
1 #include &iostream&
2 #include &cmath&
3 #include &vector&
4 #include &cstdio&
5 #include &cstring&
6 #include &cstdlib&
7 #include &string&
8 #include &sstream&
9 #include &algorithm&
10 #define Max
11 #define INF -0x7fffffff
12 #define N 910
13 #define ll long long
14 #define mem(a,b) memset(a,b,sizeof(a))
15 #define repu(i, a, b) for(int i = (a); i & (b); i++)
16 const double PI=-acos(-1.0);
17 using namespace
18 vector&pair&ll,ll& &
19 int main()
while(scanf("%d",&n) && n)
v.clear();
repu(i,0,n)
scanf("%I64d%I64d%I64d",&a,&b,&c);
vector&pair&ll,ll& & ::
if(a == 1)
v.push_back(pair&ll,ll&(b,c));
else if(a == -1)
pair&ll,ll& t = pair&ll,ll&(b,c);
for(it = v.begin(); it!=v.end(); it++)
if(*it == t)
v.erase(it);
ll maxn = 0;
int flag = 1;
for(it = v.begin(); it!=v.end(); it++)
maxn = it-&first * b + it-&second * c,flag = 0;
maxn = max(it-&first * b + it-&second * c,maxn);
printf("%I64d\n",maxn);
1 #include &iostream&
2 #include &cmath&
3 #include &list&
4 #include &cstdio&
5 #include &cstring&
6 #include &cstdlib&
7 #include &string&
8 #include &sstream&
9 #include &algorithm&
10 #define Max
11 #define INF -0x7fffffff
12 #define N 910
13 #define ll long long
14 #define mem(a,b) memset(a,b,sizeof(a))
15 #define repu(i, a, b) for(int i = (a); i & (b); i++)
16 const double PI=-acos(-1.0);
17 #define pii pair&ll,ll&
18 using namespace
19 list&pii &
20 int main()
while(scanf("%d",&n) && n)
v.clear();
///list&pii& v
每次被定义一遍更费时间,还不如clear时间少
repu(i,0,n)
scanf("%I64d%I64d%I64d",&a,&b,&c);
list&pii& ::
if(a == 1)
v.push_front(pii(b,c));
else if(a == -1)
pii t = pii(b,c);
for(it = v.begin(); it!=v.end(); it++)
if(*it == t)
v.erase(it);
///v.remove(*it);也会超时,题目卡的很严
ll maxn = 0;
int flag = 1;
for(it = v.begin(); it!=v.end(); it++)
maxn = it-&first * b + it-&second * c,flag = 0;
maxn = max(it-&first * b + it-&second * c,maxn);
printf("%I64d\n",maxn);
AC代码也是看了某个大神的代码仅仅用Ctrl+r把TLE代码中的vector改成了list,就A了,但是时间有点慢,我也是醉了;
其实就是vector和list的区别():
具体细节看链接,只写用法:
& 在实际使用时,如何选择这三个容器中哪一个,应根据你的需要而定,一般应遵循下面的原则:& 1、如果你需要高效的随即存取,而不在乎插入和删除的效率,使用vector& 2、如果你需要大量的插入和删除,而不关心随即存取,则应使用list& 3、如果你需要随即存取,而且关心两端数据的插入和删除,则应使用deque。
而这个题目,即使用vector也并不是随即随取,也得是从头到尾遍历,因此插入和删除的时间复杂度更重要一些,所以就选择了list。
list的时间复杂度:&&&&&&&& insert-------O(1)& & & & & && erase-------O(1)
vector的时间复杂度:&&&&& insert------O(log(n))&&&&&&& erase------O(log(n))java.util.vector中的vector的详细用法-android100学习网
java.util.vector中的vector的详细用法
ArrayList会比Vector快,他是非同步的,如果设计涉及到多线程,还是用Vector比较好一些 
import java.util.*;
ArrayList会比Vector快,他是非同步的,如果设计涉及到多线程,还是用Vector比较好一些&
import java.util.*;
* 演示Vector的使用。包括Vector的创建、向Vector中添加元素、从Vector中删除元素、&
* 统计Vector中元素的个数和遍历Vector中的元素。&
public class VectorDemo{&
public static void main(String[] args){
//Vector的创建&
//使用Vector的构造方法进行创建&
Vector v = new Vector(4);
//向Vector中添加元素&
//使用add方法直接添加元素&
v.add("Test0");&
v.add("Test1");&
v.add("Test0");&
v.add("Test2");&
v.add("Test2");
//从Vector中删除元素&
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));&
-------------&
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.S
import java.util.V
import java.util.E
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 中。
同步是个很大的问题,尤其多线程,和进程中,因此,我们在多线程中同时对某个数组操作时,支持同步的vector无疑是个很好的选择,一般在需要将多个元素存在一个集合里的时候用。
java.util 类 Vector&E&&
boolean add(E o)&
将指定元素追加到此向量的末尾。&
void add(int index, E element)&
在此向量的指定位置插入指定的元素。&
boolean addAll(Collection&? extends E& c)&
将指定 Collection 中的所有元素追加到此向量的末尾,按照指定集合的迭代器所返回的顺序追加这些元素。&
boolean addAll(int index, Collection&? extends E& c)&
在指定位置将指定 Collection 中的所有元素插入到此向量中。&
void addElement(E obj)&
将指定的组件添加到此向量的末尾,将其大小增加 1。&
int capacity()&
返回此向量的当前容量。&
void clear()&
从此向量中移除所有元素。&
Object clone()&
返回向量的一个副本。&
boolean contains(Object elem)&
测试指定的对象是否为此向量中的组件。&
boolean containsAll(Collection&?& c)&
如果此向量包含指定 Collection 中的所有元素,则返回 true。&
void copyInto(Object[] anArray)&
将此向量的组件复制到指定的数组中。&
E elementAt(int index)&
返回指定索引处的组件。&
Enumeration&E& elements()&
返回此向量的组件的枚举。&
void ensureCapacity(int minCapacity)&
增加此向量的容量(如有必要),以确保其至少能够保存最小容量参数指定的组件数。&
boolean equals(Object o)&
比较指定对象与此向量的相等性。&
E firstElement()&
返回此向量的第一个组件(位于索引 0 处的项)。&
E get(int index)&
返回向量中指定位置的元素。&
int hashCode()&
返回此向量的哈希码值。&
int indexOf(Object elem)&
搜索给定参数的第一个匹配项,使用 equals 方法测试相等性。&
int indexOf(Object elem, int index)&
搜索给定参数的第一个匹配项,从 index 处开始搜索,并使用 equals 方法测试其相等性。&
void insertElementAt(E obj, int index)&
将指定对象作为此向量中的组件插入到指定的 index 处。&
boolean isEmpty()&
测试此向量是否不包含组件。&
E lastElement()&
返回此向量的最后一个组件。&
int lastIndexOf(Object elem)&
返回指定的对象在此向量中最后一个匹配项的索引。&
int lastIndexOf(Object elem, int index)&
向后搜索指定的对象,从指定的索引处开始搜索,并返回一个索引。&
E remove(int index)&
移除此向量中指定位置的元素。&
boolean remove(Object o)&
移除此向量中指定元素的第一个匹配项,如果向量不包含该元素,则元素保持不变。&
boolean removeAll(Collection&?& c)&
从此向量中移除包含在指定 Collection 中的所有元素。&
void removeAllElements()&
从此向量中移除全部组件,并将其大小设置为零。&
boolean removeElement(Object obj)&
从此向量中移除变量的第一个(索引最小的)匹配项。&
void removeElementAt(int index)&
删除指定索引处的组件。&
protected void removeRange(int fromIndex, int toIndex)&
从此 List 中移除其索引位于 fromIndex(包括)与 toIndex(不包括)之间的所有元素。&
boolean retainAll(Collection&?& c)&
在此向量中仅保留包含在指定 Collection 中的元素。&
E set(int index, E element)&
用指定的元素替换此向量中指定位置处的元素。&
void setElementAt(E obj, int index)&
将此向量指定 index 处的组件设置为指定的对象。&
void setSize(int newSize)&
设置此向量的大小。&
int size()&
返回此向量中的组件数。&
List&E& subList(int fromIndex, int toIndex)&
返回此 List 的部分视图,元素范围为从 fromIndex(包括)到 toIndex(不包括)。&
Object[] toArray()&
返回一个数组,包含此向量中以正确顺序存放的所有元素。&
toArray(T[] a)&
返回一个数组,包含此向量中以正确顺序存放的所有元素;返回数组的运行时类型为指定数组的类型。&
String toString()&
返回此向量的字符串表示形式,其中包含每个元素的 String 表示形式。&
void trimToSize()&
对此向量的容量进行微调,使其等于向量的当前大小。

我要回帖

更多关于 vector nti 替代 的文章

 

随机推荐