初学者的问题:复制数组的某些元素

[英]beginner's question: copying certain elements of an array


I am trying to copy certain elements of an array into another. For example, I want to copy index 0 of lines into index 0 of links, index 3 of lines into index 1 of links, and so on (every 3 element of lines basically basically).

我试图将数组的某些元素复制到另一个元素中。例如,我想将行的索引0复制到链接的索引0,将索引3的行复制到链接的索引1,依此类推(基本上每3行元素基本上)。

What I have so far keeps getting me an ArrayIndexOutOfBound error. Thank you for your help!

到目前为止,我一直得到一个ArrayIndexOutOfBound错误。感谢您的帮助!

String[] lines = inputString.split(System.getProperty("line.separator"));
String[] links = new String[lines.length]; 
int j = 0;

for (int i = 0; i < lines.length; i++) {
   links[i] = lines[j+3];
   j++;
   System.out.println(links[i]);
}

3 个解决方案

#1


4  

It sounds like you need to be incrementing i by 3, rather than adding 3 to j (but then incrementing j by just 1). In fact, you don't need two variables at all:

听起来你需要将i递增3,而不是将j加3(但随后将j递增1)。实际上,您根本不需要两个变量:

for (int i = 0; i < lines.length; i += 3) {
    links[i / 3] = lines[i];
}

You should also change your code to only create the array to be as large as you need:

您还应该将代码更改为仅根据需要创建数组:

String[] links = new String[lines.length / 3];

Just for the sake of interest, let's have a look at what your code was actually trying to do, in terms of assignments:

仅仅为了感兴趣,让我们看一下你的代码实际上要做的事情,就任务而言:

links[0] = lines[3];
links[1] = lines[4];
links[2] = lines[5];
// etc

As you can see, this is just offsetting the index, rather than multiplying it... and as soon as i was lines.length-3, you'd end up with

正如你所看到的,这只是抵消了索引,而不是乘以它...而且只要我是lines.length-3,你最终会得到它

links[lines.length - 3] = lines[lines.length]; // Bang!

#2


0  

A few problems:

一些问题:

1. You probably want this:

你可能想要这个:

for (int i = 0; i < lines.length; i++) {
    links[i] = lines[j+3];

To look like this:

看起来像这样:

for (int i = 0; i < links.length; i++) {
    links[i] = lines[j * 3];

2. Well, yes. You are going out of bounds. Let's say lines is 12 elements. links is the same size, so you're trying to read from element 15 / 36 (depending on my correction in #1)

嗯,是的。你出局了。我们说线条是12个元素。链接大小相同,所以你试图从元素15/36读取(取决于我在#1中的更正)

The solution is to make links smaller:

解决方案是使链接更小:

String[] links = new String[(int)(lines.length / 3)];

Along with the correction in #1.

随着#1的修正。

#3


0  

Jon Skeet has the right idea. What might be useful is to learn how to debug these things yourself. Say that lines.length is 5, meaning also that links.length is 5. What happens if you trace every iteration of the for loop?

Jon Skeet有正确的想法。可能有用的是学习如何自己调试这些东西。假设lines.length是5,也就是说links.length是5.如果跟踪for循环的每次迭代会发生什么?

i        |      j       |     j + 3       
-----------------------------------------
0               0               3       
1               1               4
2               2               5

It'll break on this 3rd iteration. You can see that you're not accessing every three elements at all, but every element, and you're storing it in an offset position, which causes your index out of bounds exception.

它会在第3次迭代中破裂。您可以看到您根本不访问每三个元素,而是访问每个元素,并将它存储在偏移位置,这会导致索引超出范围异常。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/08/10/93ed289a5cd9d8494aa39cbf053f0ba3.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com