如何在Java中增加数组的大小?
我希望在一个数组中存储用户所需的任意多个元素。但是我该怎么做呢 如果要创建数组,必须使用固定大小。每次将新元素添加到数组中并且数组变满时,我都希望将其大小更新为“1” 我厌倦了各种类型的代码,但没有成功如何在Java中增加数组的大小?,java,arrays,size,Java,Arrays,Size,我希望在一个数组中存储用户所需的任意多个元素。但是我该怎么做呢 如果要创建数组,必须使用固定大小。每次将新元素添加到数组中并且数组变满时,我都希望将其大小更新为“1” 我厌倦了各种类型的代码,但没有成功 如果有人能给我一个关于它的解决方案,那将是非常有帮助的。如果可能的话,在代码中。使用ArrayList。如果尝试添加到完整的ArrayList,它会自动增加的大小。请使用java.util.List的实现,而不是使用数组,例如ArrayList。ArrayList有一个数组后端,它保存列表中的值
如果有人能给我一个关于它的解决方案,那将是非常有帮助的。如果可能的话,在代码中。使用ArrayList。如果尝试添加到完整的ArrayList,它会自动增加的大小。请使用
java.util.List
的实现,而不是使用数组,例如ArrayList
。ArrayList有一个数组后端,它保存列表中的值,但数组大小由列表自动处理
ArrayList<String> list = new ArrayList<String>();
list.add("some string");
ArrayList list=new ArrayList();
添加(“一些字符串”);
对于其他元素类型,您还可以使用
list.toArray(新字符串[list.size()])
等将列表转换为数组。u可以使用数组列表进行转换
下面是字符串数组列表的示例
'导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.ArrayList
公共类Ex01{
public static void main(String[] args) throws IOException {
BufferedReader userInput = new BufferedReader
(new InputStreamReader(System.in));
ArrayList<String> myArr = new ArrayList<String>();
myArr.add("Italian Riviera");
myArr.add("Jersey Shore");
myArr.add("Puerto Rico");
myArr.add("Los Cabos Corridor");
myArr.add("Lubmin");
myArr.add("Coney Island");
myArr.add("Karlovy Vary");
myArr.add("Bourbon-l'Archambault");
myArr.add("Walt Disney World Resort");
myArr.add("Barbados");
System.out.println("Stupid Vacation Resort Adviser");
System.out.println("Enter your name:");
String name = userInput.readLine();
Integer nameLength = name.length();
if (nameLength == 0)
{
System.out.println("empty name entered");
return;
}
Integer vacationIndex = nameLength % myArr.size();
System.out.println("\nYour name is "+name+", its length is " +
nameLength + " characters,\n" +
"that's why we suggest you to go to "
+ myArr.get(vacationIndex));
}
publicstaticvoidmain(字符串[]args)抛出IOException{
BufferedReader userInput=新BufferedReader
(新的InputStreamReader(System.in));
ArrayList myArr=新的ArrayList();
myArr.add(“意大利里维埃拉”);
myArr.添加(“泽西海岸”);
myArr.增补(“波多黎各”);
myArr.add(“洛斯卡沃斯走廊”);
myArr.添加(“Lubmin”);
myArr.add(“科尼岛”);
myArr.添加(“Karlovy Vary”);
myArr.add(“波旁-拉汉堡”);
myArr.add(“沃尔特迪斯尼世界度假村”);
myArr.add(“巴巴多斯”);
System.out.println(“愚蠢的度假胜地顾问”);
System.out.println(“输入您的姓名:”);
String name=userInput.readLine();
整数nameLength=name.length();
如果(nameLength==0)
{
System.out.println(“输入空名称”);
返回;
}
整数vacationIndex=nameLength%myArr.size();
System.out.println(“\n您的名称为“+name+”,长度为“+
nameLength+“个字符,\n”+
“这就是为什么我们建议你去”
+myArr.get(假期指数);
}
}"
类似地,u可以为integer、blooean和各种数据类型创建数组
更多细节,你可以看到这个
你不能。您可以创建一个新数组并将项目移动到该数组,也可以使用ArrayList。创建列表对象,添加元素并使用list.toArray(新字符串[list.size()])将该列表对象转换为数组。您可以创建一个临时数组,其大小比原始数组大一个元素,然后将原始数组的元素复制到临时数组中,并将临时数组分配给新数组
public void increaseSize() {
String[temp] = new String[original.length + 1];
for (int i = 0; i < original.length; i++){
temp[i] = original[i];
}
original = temp;
}
public void increaseSize(){
字符串[temp]=新字符串[original.length+1];
对于(int i=0;i
您可以通过各种方式更改大小,但同样的想法也适用。通过使用
java.util.Arrays
classString[]中的copyOf方法,大小将自动/动态增加。在下面的代码array
size0
中,在使用数组进行操作后。copyOf
字符串数组的大小增加到4
package com.google.service;
import java.util.Arrays;
public class StringArrayAutoIncrement {
public static void main(String[] args) {
String[] data = new String[] { "a", "b", "c", "d", "e" };
String[] array = new String[0];// array initialize with zero
int incrementLength = 1;
for (int i = 0; i < data.length; i++) {
array = Arrays.copyOf(data, i + incrementLength);// increment by +1
}
/**
* values of array after increment
*/
for (String value : array) {
System.out.println(value);
}
}
}
在较低级别上,您可以通过以下方式完成:
long[] source = new long[1];
long[] copy = new long[source.length + 1];
System.arraycopy(source, 0, copy, 0, source.length);
source = copy;
Arrays.copyOf()也在做同样的事情。公共类递增ArraySize{
public class IncreaseArraySize {
public static void main(String[] args) {
int arr[] = new int[5];
for (int i = 0; i < arr.length; i++) {
arr[i] = 5;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println(" ");
System.out.println("Before increasing Size of an array :" + arr.length);
int arr2[] = new int[10];
arr = arr2;
arr2 = null;
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
System.out.println("After increasing Size of an array : " + arr.length);
}
}
公共静态void main(字符串[]args){
int arr[]=新的int[5];
对于(int i=0;i
你可以这样做
import java.util.Scanner;
class Add
{
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
char ans='y';
int count=0;
while(ans!='N'||ans!='n')
{
int initial_size=5;
int arr[]=new int [initial_size];
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
arr[4]=5;
if(count>0)
{
System.out.print("enter the element u want to add in array: ");
arr[initial_size-1]=obj.nextInt();
}
System.out.print("Do u want to add element in array?");
ans=obj.next().charAt(0);
if(ans=='y'||ans=='Y')
{
initial_size++;
count++;
}
}
}
}
您还可以执行myArr=Arrays.copyOf(myArr,10)
在本例中,do,myArr=Arrays.copyOf(myArr,myAry.length+1) 谢谢…这…太好了…如果你能给我打印出来的代码..中的元素list@joelabishaipaulSystem.out.println(list)
应该可以很好地完成这项工作;)如果您想在单独的一行上打印每个值,可以使用a对列表中的每个值进行迭代。参考此链接,它会给出一些想法,但这不是答案。令人惊讶的是,这是有效的。我想知道为什么没有人对你的答案投赞成票
import java.util.Scanner;
class Add
{
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
char ans='y';
int count=0;
while(ans!='N'||ans!='n')
{
int initial_size=5;
int arr[]=new int [initial_size];
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
arr[4]=5;
if(count>0)
{
System.out.print("enter the element u want to add in array: ");
arr[initial_size-1]=obj.nextInt();
}
System.out.print("Do u want to add element in array?");
ans=obj.next().charAt(0);
if(ans=='y'||ans=='Y')
{
initial_size++;
count++;
}
}
}
}
int[] myArr = {2,4,2,4,5,6,3};
System.out.println("Array size before copy: "+myArr.length);
int[] newArr = Arrays.copyOf(myArr, 10);
System.out.println("New array size after copying: "+newArr.length);