I'm trying to use to store info from a text file into a array, but the project asks us to do so by making a temporary array and increasing its size as we read more files to accomadate for the new objects. How can I do this?
我正在尝试使用将文本文件中的信息存储到数组中,但项目要求我们这样做,方法是创建一个临时数组并增加其大小,因为我们会读取更多文件以适应新对象。我怎样才能做到这一点?
/**
* 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
}
3 个解决方案
#1
4
If your teacher requires a specific method you should probably have listened more closely in class :-)
如果您的老师需要特定的方法,您可能应该在课堂上更密切地倾听:-)
You can grow an array by one like this:
您可以像这样增加一个数组:
myArray = Arrays.copyOf(myArray,myArray.length+1);
but there is no for-loop to be seen. You can do it using older Java methods like this:
但是没有可以看到的循环。你可以使用像这样的旧Java方法来做到这一点:
Object[] tmpArray = new Object[myArray.length+1];
System.arraycopy(myArray,0,tmpArray,0,myArray.length);
myArray = tmpArray;
Again, no for-loop required. The only advantage is that it runs with Java 1.5. The only "need" for a for-loop in this question is to do what System.arraycopy
(which is used by Arrays.copyOf
) does more efficiently. Like this:
同样,不需要for-loop。唯一的优点是它运行Java 1.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;
The idea is thus to give you practice in using for-loops, not to actually solve the problem in a good way.
因此,这个想法是让你练习使用for循环,而不是以一种好的方式实际解决问题。
The easiest way to grow an array as you use it is to use some form of List
, java.util.ArrayList
for example, let the list handle storing the data, and invoke the List's toArray()
method when you are finished.
在使用数组时增长数组的最简单方法是使用某种形式的List,例如java.util.ArrayList,让列表句柄存储数据,并在完成后调用List的toArray()方法。
#2
0
Use
Arrays.copyOfRange(T[] original,int from, int to)
method to copy one array
to diffrent array
将一个数组复制到不同数组的方法
#3
0
I'm going to ignore the fact that the requirement to use arrays for this makes no sense whatsoever. What you want to do is assign your initial array a given size, and increase it by a set amount whenever you reach the end of the storage space. Typically, you would increase the underlying storage by a factor of two, whenever you run out of space. Heres some pseudocode:
我将忽略这样一个事实,即为此使用数组的要求毫无意义。您要做的是将初始数组指定给定大小,并在到达存储空间末尾时将其增加一定量。通常,只要空间不足,就可以将基础存储增加两倍。下面是一些假代码:
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;
}
With some slight modifications, the above code should work for you. Study it, embed it, but then totally ignore it and if you ever have a future need for expanding arrays please use ArrayList instead. Or System.arraycopy.
稍作修改,上面的代码应该适合您。研究它,嵌入它,但然后完全忽略它,如果你将来需要扩展数组,请使用ArrayList。或System.arraycopy。
#1
4
If your teacher requires a specific method you should probably have listened more closely in class :-)
如果您的老师需要特定的方法,您可能应该在课堂上更密切地倾听:-)
You can grow an array by one like this:
您可以像这样增加一个数组:
myArray = Arrays.copyOf(myArray,myArray.length+1);
but there is no for-loop to be seen. You can do it using older Java methods like this:
但是没有可以看到的循环。你可以使用像这样的旧Java方法来做到这一点:
Object[] tmpArray = new Object[myArray.length+1];
System.arraycopy(myArray,0,tmpArray,0,myArray.length);
myArray = tmpArray;
Again, no for-loop required. The only advantage is that it runs with Java 1.5. The only "need" for a for-loop in this question is to do what System.arraycopy
(which is used by Arrays.copyOf
) does more efficiently. Like this:
同样,不需要for-loop。唯一的优点是它运行Java 1.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;
The idea is thus to give you practice in using for-loops, not to actually solve the problem in a good way.
因此,这个想法是让你练习使用for循环,而不是以一种好的方式实际解决问题。
The easiest way to grow an array as you use it is to use some form of List
, java.util.ArrayList
for example, let the list handle storing the data, and invoke the List's toArray()
method when you are finished.
在使用数组时增长数组的最简单方法是使用某种形式的List,例如java.util.ArrayList,让列表句柄存储数据,并在完成后调用List的toArray()方法。
#2
0
Use
Arrays.copyOfRange(T[] original,int from, int to)
method to copy one array
to diffrent array
将一个数组复制到不同数组的方法
#3
0
I'm going to ignore the fact that the requirement to use arrays for this makes no sense whatsoever. What you want to do is assign your initial array a given size, and increase it by a set amount whenever you reach the end of the storage space. Typically, you would increase the underlying storage by a factor of two, whenever you run out of space. Heres some pseudocode:
我将忽略这样一个事实,即为此使用数组的要求毫无意义。您要做的是将初始数组指定给定大小,并在到达存储空间末尾时将其增加一定量。通常,只要空间不足,就可以将基础存储增加两倍。下面是一些假代码:
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;
}
With some slight modifications, the above code should work for you. Study it, embed it, but then totally ignore it and if you ever have a future need for expanding arrays please use ArrayList instead. Or System.arraycopy.
稍作修改,上面的代码应该适合您。研究它,嵌入它,但然后完全忽略它,如果你将来需要扩展数组,请使用ArrayList。或System.arraycopy。