日期:2014-05-20  浏览次数:20700 次

一个java数组问题,,,,
为何我定义了一个arr[10][变化值]的数组,用arr[i].length得到的长度却是10呢,我想得到 "变化值 "啊

------解决方案--------------------
循环啊!

------解决方案--------------------
我觉得不是特殊需要就不用数组
用ArrayList(就是数组) 和 vector 比较好
------解决方案--------------------
Object[][] o=new Object[10][5];
o[0].length 出来的就是5
------解决方案--------------------
学习
------解决方案--------------------
"arr[i].length "对于每一个i都有一个长度对应
------解决方案--------------------
如果是出于性能考虑的话,还是优先使用array吧,其次是ArrayList,最后才是Vector
可以参考Practical Java的相关论述
------解决方案--------------------
int[][] arr={
{1,2,3,4,5,6},
{1,2,3,4},
{1,2,3,4,5,6,7,8,8}
};
中,
arr[0].length=6
arr[1].length=4
------解决方案--------------------
ArrayList用法(转载)
System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。

一.优点

1。支持自动改变大小的功能
2。可以灵活的插入元素
3。可以灵活的删除元素

二.局限性

跟一般的数组比起来,速度上差些

三.添加元素

1.publicvirtualintAdd(objectvalue);

将对象添加到ArrayList的结尾处

ArrayListaList=newArrayList();
aList.Add( "a ");
aList.Add( "b ");
aList.Add( "c ");
aList.Add( "d ");
aList.Add( "e ");
内容为abcde

2.publicvirtualvoidInsert(intindex,objectvalue);

将元素插入ArrayList的指定索引处

ArrayListaList=newArrayList();
aList.Add( "a ");
aList.Add( "b ");
aList.Add( "c ");
aList.Add( "d ");
aList.Add( "e ");
aList.Insert(0, "aa ");

结果为aaabcde

3.publicvirtualvoidInsertRange(intindex,ICollectionc);

将集合中的某个元素插入ArrayList的指定索引处

ArrayListaList=newArrayList();
aList.Add( "a ");
aList.Add( "b ");
aList.Add( "c ");
aList.Add( "d ");
aList.Add( "e ");
ArrayListlist2=newArrayList();
list2.Add( "tt ");
list2.Add( "ttt ");
aList.InsertRange(2,list2);

结果为abtttttcde

四.删除

a)publicvirtualvoidRemove(objectobj);

从ArrayList中移除特定对象的第一个匹配项,注意是第一个

ArrayListaList=newArrayList();
aList.Add( "a ");
aList.Add( "b ");
aList.Add( "c ");
aList.Add( "d ");
aList.Add( "e ");
aList.Remove( "a ");

结果为bcde

2.publicvirtualvoidRemoveAt(intindex);

移除ArrayList的指定索引处的元素

aList.Add( "a ");
aList.Add( "b ");
aList.Add( "c ");
aList.Add( "d ");
aList.Add( "e ");
aList.RemoveAt(0);

结果为bcde

3.publicvirtualvoidRemoveRange(intindex,intcount);

从ArrayList中移除一定范围的元素。Index表示索引,count表示从索引处开始的数目

aList.Add( "a ");
aList.Add( "b ");
aList.Add( "c ");
aList.Add( "d ");
aList.Add( "e ");
aList.RemoveRange(1,3);

结果为ae

4.publicvirtualvoidClear();

从ArrayList中移除所有元素。

五.排序

a)publicvirtualvoidSort();

对ArrayList或它的一部分中的元素进行排序。

ArrayListaList=newArrayList();