如何创建一个更大的数组,并复制java中以前数组中的信息?
我试图使用它将文本文件中的信息存储到数组中,但项目要求我们创建一个临时数组,并在读取更多文件以适应新对象时增加其大小。我该怎么做如何创建一个更大的数组,并复制java中以前数组中的信息?,java,arrays,Java,Arrays,我试图使用它将文本文件中的信息存储到数组中,但项目要求我们创建一个临时数组,并在读取更多文件以适应新对象时增加其大小。我该怎么做 /** * reads a csv data file and returns an array of acquaintances * @param path File path of CSV file * @return Acquaintances from the file */ public static Acquaintance[] read (Str
/**
* reads a csv data file and returns an array of acquaintances
* @param path File path of CSV file
* @return Acquaintances from the file
*/
public static Acquaintance[] read (String path) {
//create an array of acquaintances
Acquaintance[] acqs = new Acquaintance[0];
//open the file
try {
BufferedReader file = new BufferedReader(new FileReader(path));
//read the file until the end
String line;
while( (line = file.readLine()) != null) {
// parse the line just read
String[] parts = line.split(",");
//create an acquaintance object
Acquaintance a = new Acquaintance(parts[0], parts[1], Double.parseDouble(parts[2]));
acqs[0] = a;
//Add the object to the array
//(1) create a new Acquaintance array, with one extra element
Acquaintance[] tmp = new Acquaintance[acqs.length+1];
//(2) copy all old elements into new
Acquaintance[] tmp = acqs.clone();
//(3) assign new Acquaintance object to last element of the array
//(4) assign new array's address to acqs
//for loop
}
使用
方法将一个
数组
复制到不同的数组
如果您的老师需要特定的方法,您可能应该在课堂上更仔细地听:-)
可以按如下方式逐个增长阵列:
myArray = Arrays.copyOf(myArray,myArray.length+1);
Object[] tmpArray = new Object[myArray.length+1];
System.arraycopy(myArray,0,tmpArray,0,myArray.length);
myArray = tmpArray;
但是,我们看不到for循环。您可以使用以下较旧的Java方法进行此操作:
myArray = Arrays.copyOf(myArray,myArray.length+1);
Object[] tmpArray = new Object[myArray.length+1];
System.arraycopy(myArray,0,tmpArray,0,myArray.length);
myArray = tmpArray;
同样,不需要for循环。唯一的优点是它使用Java1.5运行。在这个问题中,for循环的唯一“需要”是做System.arraycopy
(由Arrays.copyOf
使用)更有效的事情。像这样:
Object[] tmpArray = new Object[myArray.length+1];
for(int i=0;i<myArray.length;i++) tmpArray[i]=myArray[i];
myArray = tmpArray;
Object[]tmpArray=新对象[myArray.length+1];
对于(int i=0;i我将忽略这样一个事实,即为此使用数组的要求毫无意义。您要做的是为初始数组指定一个给定的大小,并在到达存储空间末端时将其增加一个设定的量。通常,每当运行这是一些伪码:
public Acquaintance[] getAcquaitances(final String path) {
if(path == null) return null;
int count = 0, initialSize = 16;
Acquaintance[] temporaryArr = new Acquaintance[initialSize];
// Open file here
String line = null;
while((line = file.readLine()) != null) {
if(count >= temporaryArr.length) {
Acquaintance[] switch = temporaryArr;
temporaryArr = new Acquaintance[switch.length * 2] // increase size by factor of 2
for(int i = 0; i < switch.length; i++) {
temporaryArr[i] = switch[i];
}
}
temporaryArr[count] = createAcquantainceFromLine(line);
++count;
}
Acquaintance[] results = new Acquaintance[count];
for(int i = 0; i < count; i++) {
results[i] = temporaryArr[i];
}
return results;
}
public熟人[]获取资格(最终字符串路径){
if(path==null)返回null;
int count=0,initialSize=16;
熟人[]临时ARR=新熟人[初始大小];
//在这里打开文件
字符串行=null;
而((line=file.readLine())!=null){
如果(计数>=临时阵列长度){
熟人[]开关=临时ARR;
temporaryArr=新认识[switch.length*2]//将大小增加2倍
对于(int i=0;i
稍微修改一下,上面的代码应该适合你。研究它,嵌入它,然后完全忽略它,如果你将来需要扩展数组,请改用它。或者。这似乎是家庭作业?检查ArrayList
的ensurecapacity
方法:在有人完成后不要删除这个问题给出了答案。这对其他所有人都没用。老师说使用for循环。我如何合并它,使数组比以前大1倍,然后又将旧数组转移到新数组,然后将新数组指定为旧数组?copyOfRange
方法只能使数组变小或变小a保持其大小。它不能使数组按问题要求变大。这是一个完美的答案@Simon,谢谢!您的第一个示例使添加索引值的对象数组中添加新对象变得非常简单。