「动态数组java」动态数组内建函数
本篇文章给大家谈谈动态数组java,以及动态数组内建函数对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、JAVA中如何动态改变数组长度 动态数组 arraycopy
- 2、java 动态数组问题
- 3、java中动态数组运用
- 4、如何用java语言创建不定长动态数组
- 5、java如何定义和使用动态数组?
JAVA中如何动态改变数组长度 动态数组 arraycopy
java中的数组一旦被定义长度是不允许修改长度的,如果题主希望实现类似的功能可以采用ArrayList。ArrayList的底层结构就是数组。当ArrayList实例化时会默认设置一个长度,当长度不够用时会自动扩展现有长度的1/2.(假设初始数组长度为10,现在变成了15)
System.arraycopy(Object src,int srcPos,Object dest,int destPos,int length )能够实现数组复制的功能。
假设有数组a(src),数组b(dest),我们希望把a数组中从下标为2(srcPos)长度为3(length)的元素复制到b的下标为4(destPos)开始的元素中。java代码示例如下:
int a = [1,3,5,7,9];
int b = [2,4,6,8,0,2,4];
System.arraycopy(a,2,b,4,3);
//最终b的内容为[2,4,6,8,5,7,9];
但是如果注意API的话会发现arraycopy有几个限制
1、a,b的元素类型必须相同(int和Integer这里也是需要区分的)
2、数组不能越界,也就是srcPos+length不能超过a的长度;desPos+length也不能超过b的长度,否则就会报数组越界的一场。这也说明了无法实现数组长度的动态扩展。
java 动态数组问题
import java.util.ArrayList;
import java.util.Iterator;
public class TestList {
String a="1";
String b="2";
String c="3";
public ArrayList setList(){
ArrayList list=new ArrayList();
list.add(a);
list.add(b);
list.add(c);
return list;
}
public void getList(ArrayList list){
Iterator iterList=list.iterator();
int i=0;
while(iterList.hasNext()){
iterList.next();
System.out.println(list.get(i));
i++;
}
}
}
import java.util.ArrayList;
public class Test {
void getlist(){
TestList list=new TestList();
ArrayList arrayList=list.setList();
list.getList(arrayList);
}
public static void main(String[] args){
Test test=new Test();
test.getlist();
}
}
java中动态数组运用
import java.util.ArrayList;
public class TestMain {
public static void main(String[] args) {
ArrayListString a=new ArrayListString();
System.out.println("-----------------增----------------------------");
a.add("测试1");
a.add("测试2");
a.add("测试3");
System.out.println("动态数组的长度为:"+a.size());
System.out.println("动态数组增加的元素为:"+a.get(0));
System.out.println("数组中 的内容为:"+getAll(a));
System.out.println("-----------------删----------------------------");
System.out.println("即将要删除的元素为:"+a.get(a.size()-1));
a.remove(a.size()-1);
System.out.println("删除后动态数组的长度为:"+a.size());
System.out.println("删除后数组中 的内容为:"+getAll(a));
System.out.println("-----------------改----------------------------");
a.set(0, "修改");
System.out.println("动态数组的长度为:"+a.size());
System.out.println("修改后数组中 的内容为:"+getAll(a));
}
public static String getAll(ArrayListString a){
StringBuffer bf=new StringBuffer();
for(int i=0;ia.size();i++){
if(ia.size()-1){
bf=bf.append(a.get(i));
bf.append(" || ");
}else{
bf=bf.append(a.get(i));
}
}
return bf.toString();
}
}
辛苦自己打得 希望对你有帮助
如何用java语言创建不定长动态数组
public class intArray {
private int theSize=0;;
private int a_int[]={};
public intArray(int a[]){
this.a_int=a;
this.theSize=a.length;
}
public intArray(){
this.theSize=0;
}
public int getSize(){
return this.theSize;
}
public int getAt(int nIndex){
return this.a_int[nIndex];
}
public void SetAt(int nIndex , int newElement){
this.a_int[nIndex] =newElement;
}
public void add(int nIndex){
int old[] = this.a_int;
this.a_int= new int[this.theSize+1];
for(int i =0;iold.length;i++){
a_int[i]= old[i];
}
if(this.theSize0){
a_int[this.theSize]=nIndex;
}else{
a_int[0]=nIndex;
}
this.theSize++;
}
public static void main(String args[]){
intArray array = new intArray();
array.add(1);
array.add(2);
array.add(3);
array.add(4);
array.add(5);
for(int i = 0;iarray.getSize();i++){
System.out.println(array.getAt(i));
}
}
}
再给个任意类型的
public class myArray AnyType{
private int theSize=0;
private AnyType theItem[]=null;
public myArray(AnyType a[]){
this.theItem=a;
this.theSize=a.length;
}
public myArray(){
}
public int getSize(){
return this.theSize;
}
public AnyType get(int nIndex){
return this.theItem[nIndex];
}
public void Set(int nIndex , AnyType newElement){
this.theItem[nIndex] =newElement;
}
public void add(AnyType newVal){
AnyType old[]=this.theItem;
this.theItem= (AnyType[]) new Object[theSize+1];
if(theSize!=0){
for(int i =0;iold.length;i++){
this.theItem[i]= old[i];
}
}
this.theItem[this.theSize]=newVal;
this.theSize++;
}
public static void main(String args[]){
myArrayString array = new myArrayString();
array.add("1");
array.add("2");
array.add("3");
array.add("a");
array.add("b");
array.add("c");
for(int i =0;iarray.theSize;i++){
System.out.println(array.get(i));
}
}
}
java如何定义和使用动态数组?
Vector类型
label=new
Vector类型();//定义一个动态存储的变量
label.add("Hello");//例如是字符串
关于动态数组java和动态数组内建函数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。