在Android SDK API中是这样描述ArrayList<E>这个类的,如下:

ArrayList is an implementation of List, backed by an array. All optional operations including adding, removing, and replacing elements are supported.All elements are permitted, including null.

This class is a good choice as your default List implementation.Vector synchronizes all operations, but not necessarily in a way that's meaningful to your application: synchronizing each call toget, for example, is not equivalent to synchronizing the list and iterating over it (which is probably what you intended).CopyOnWriteArrayList is intended for the special case of very high concurrency, frequent traversals, and very rare mutations.

下面来看看其中一些常用方法:

1、<T> T[] toArray(T[] contents)

当声明了一个ArrayList<E>动态数组后,并添加了自己需要的数据后,想把这样的一个ArrayList<E>数组转换为普通的数组,如Integer[] TestArray = new Integer[100],这时可以采用以下方法,如下实例:

public class TestActivity extends Activity {

ArrayList<Byte> arrayList;
Byte[] testArray;
Byte[] testArray1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

arrayList = new ArrayList<Byte>();
testArray = new Byte[100];

testArray[0] = 0x01;
testArray[1] = 0x02;
testArray[2] = 0x03;

arrayList.add(testArray[0]);
arrayList.add(testArray[1]);
arrayList.add(testArray[2]);


testArray1 = new Byte[arrayList.size()]; //最终转换成的目标普通数组
arrayList.toArray(testArray1);//转换为普通数组,这样就可以用普通数组下标形式访问元素了

System.out.println("arrayList.get(0)="+arrayList.get(0));
if(testArray1[0].byteValue() == 0x01)
System.out.println("arrayList.get(0)="+testArray1[2]);
}

}
。。。。待续。。。。

更多相关文章

  1. 如何在Android 11 中正确请求位置权限?以及Android 8 - 11位置权
  2. 详解Android中IntentService的使用方法
  3. ReactNative调用Android原生方法
  4. Cocos项目避免不断复制粘贴android lib库的方法
  5. Android去除系统自带动画的两种方法
  6. android studio中建立assets和jnilibs的方法
  7. Android unspecified' depends on one or more Android Librarie

随机推荐

  1. cr是什么意思?
  2. srand(time(0))函数是什么意思
  3. for(;;)什么意思
  4. if是什么意思
  5. 字符串赋值是什么意思
  6. c语言交换两个数的值
  7. remove是什么意思?
  8. 学好c语言要看什么书
  9. 学好c语言能做什么职业
  10. c如何和mysql连接