范文无忧网范文学习范文大全

如何创建ArrayList数组

03月03日 编辑 fanwen51.com

[c语言中的数组排序]#include #include int numSort(int *a,int count_num) { int i,j,min=-1,temp; for(j=count_num-1;j>0;j--) for(i=j-1;i>=0;i--) if(a[j] > a[i]) { temp=a[i]; a[i]=a[j]...+阅读

如何创建ArrayList数组

ArrayList 可以方便的实现列表操作, 但有时候需要建立一个ArrayList数组.

首先想到的是类似下面的方法:

ArrayList

但会出现错误.

改为

ArrayList[] list = new ArrayList[N];会有警告内.

这是因为Java没有范型数组,可以参考以下容方法实现类似功能:

ArrayList

ArrayList

ArrayList

ArrayList

用ArrayList的方法实现:使用一个数组储存ArrayList的元素

import java.util.*;

public class ArrayList {

private int size;

private int[] array;

private final int DEFAULT_LENGTH = 10;

public ArrayList() {

size = 0;

array = new int[DEFAULT_LENGTH];

}

public void add(int element) {

ensureCapacity();

this.array[this.size++] = element;

}

public Object get(int index) {

if (index < 0 || index > size) {

throw new IndexOutOfBoundsException("数组索引错误:" + index);

}

return this.array[index];

}

public int size() {

return this.size;

}

private void ensureCapacity() {

if (size == array.length) {

int[] longerArray = new int[size * 2];

System.arraycopy(array, 0, longerArray, 0, size);

this.array = longerArray;

}

}

Override

public String toString() {

return Arrays.toString(array);

}

public static void main(String[] args) {

ArrayList arrayList = new ArrayList();

//填入10个数字

for (int i = 0; i < 10; i++) {

arrayList.add(i);

}

//输出

System.out.println(arrayList);

//再填入10个数字

for (int i = 0; i < 10; i++) {

arrayList.add(i);

}

//输出

System.out.println(arrayList);

System.out.println(arrayList.get(12));

}

}

麻烦问一下怎样用Java实现arraylist类就是编写语句来代替这个类的功

给你个参考吧,其他方法类似,先写这三个吧

public class ArrayList{

Object[] objects=new Object[20];

int index=0;

//add方法

public void add(Object o){

if(index==objects.length){

Object[] newObjs=new Object[objects.length*2];

System.arraycopy(objects,0,newObjs,0,objects.length);

objects=newObjs;

}

objects[index]=o;

index++;

}

//size方法

public int size(){

return index;

}

//removeAt方法

public void removeAt(int index){

Object[] newO = new Object[objects.length-1];

System.arraycopy(objects, 0, newO, 0, index);

System.arraycopy(objects, index+1, newO, index, newO.length-index);

}

}

延伸阅读:

C语言数组排序#include<stdio.h> void main() { int a[10] = { 10,2,3,4,5,6,9,8,7,1 }; int i,j,t; for(j=0;j<10;j++) for(i=0;i<10-1-j;i++) if(a[i]>a[i+1]) /* 由小到大,由大到小时改...

C语言数组排序高手快来#include "stdio.h" #define N 4 void main() { long num[N]; float score[N],sum=0,average; int a,b,i,j; printf("please input student number:\n"); for(i=0;i scanf("%ld",&...

数组排序C语言#include <stdio.h> #include <iostream.h> #include <stdlib.h> #include<time.h> void main() { int a[100],i,c,b,d,e,n; cin>>n; srand((unsigned)time(NULL)); for(i=0...

C语言数组排列怎么做最常用的就是冒泡排序的方法了。 比如对10个数字进行排序,则程序是这样的 #include<stdio.h>void main(){int a[10];int i,j, t;printf("输入十个数字:\n");for(i=0;i<=9;i++){sca...

C语言数组排序方法像是选择法排序,但不太简练! 正确的选择法为: #include <stdio.h> void main(void) { int a[9]={3,42,55,546,43,323,54,121,32},i,j,l,temp; for(i=0;i<9;i++) for(j=i+1;j<8;...

c语言整数数组排序#include"stdio.h" #define N 10 void sort(int a[],int method) { int i,k,t,j; switch(method) { case 1: for(i=0;i<N;i++) { for(j=0;j<N;j++) { if(a[j]>a[i]) { t=a[i];...

设计一个数组类模板Array我给你写好了,测试过了,但是我值测试一部分,其他的你自己测试把,要是哪里不明白,或者是不符合你要求的给我发站内信,我再给你改。 代码: #include <iostream.h> #include <process....

论述:如何创建文明校园?如何创建文明学生“和谐”是我国的传统文化中具有代表性的观念,是事物存在的最佳形态,是一切美好事物的共同特点.而创建和谐校园是一个永恒的主题。 构建和谐校园需建立和谐的师生关系。师者,传...

编写一个学生类将学生的姓名年龄性别分别保存到ArrayListpackage src; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Test2 { SuppressWarnings("unche...

推荐阅读
图文推荐
栏目列表