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).
What I have so far keeps getting me an ArrayIndexOutOfBound error. Thank you for your help!
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]);
}
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:
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
links[lines.length - 3] = lines[lines.length]; // Bang!
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)
The solution is to make links smaller:
String[] links = new String[(int)(lines.length / 3)];
Along with the correction in #1.
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?
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.
Related
I'm tasked with a perplexing problem. I'm attempting to turn an unsorted array with duplicates into a sorted array without duplicates. I used the selection sort to accomplish the first part:
public static void SelectionSort(int [] list) {
int i = 0;
int j = 0;
int indexSmallest = 0;
int temp = 0;
for (i = 0; i < list.length - 1; i++) {
indexSmallest = i;
for (j = i + 1; j < list.length; j++) {
if (list[j] < list[indexSmallest]) {
indexSmallest = j;
}
}
temp = list[i];
list[i] = list[indexSmallest];
list[indexSmallest] = temp;
}
}
The sorting isn't the issue - I'm having a hard time removing the duplicates from the array. The solution that I have in my head is to create a secondary array, and iterate through the input to check if that element exists in the second array, if not, add it to the array. I'm stuck because I'm not able to create another array. So, what gives? How do I solve this problem if I can't create an array to cross-reference and check if I have unique values? I'm not able to use any built-in Java functions.
There is no need of second array ... think about it this way ...
The easiest way is to convert it to set / hashset and then sort it in an array.
But if the sets are forbidden, the only possibility is to put the duplicates at the end, cut them out and then sort the rest.
[1,8,1,2,3,5,3] in this array, you need to remove elements that are duplicates ... okay ... so what if we did something like this ... we will "split" this array into "sorted", "unsorted and duplicates" and "duplicates". Now what we will do is that we will go through the array using 2 pointers. One at the first element (lets call it "i") and at the last element (lets call it "j") ... now we will go while i < j and we will swap everytime, when we will find a duplicate. This way, you will get everything not duplicate before "i" and everything that is dupicate after "i" ... now you will sort the array from index 0 do index i and you should have sorted array and you will just cut out the duplicates ...
ofc., this will require the time complexity, to be able to handle O(n*logn) / O(n^2) ...
There is a way how to do it in a O(n), and that can be done by that .. you will use 2 pointers ...
one will be pointing at current sorted array, where you have no duplicates and toher will be pointing to a place, where are yet unswapped integers ... (you need to remember the highest number found)
to be more specific:
[1,2,2,3,3,4,5]
i = 0, j = 1
- fine
i = 1, j = 2
- duplicate ... soo ..
jumping to duplicate position
i = 2, j = 3 (array[3] != 2, so we will swap)
current array -> [1,2,3,2,3,4,5]
^ ^
i j
i = 3, j = 4
- highest_number > 3 is not true (2 < 3), so skipping
i = 3, j = 5
- highest_number > 3 is not true (3 < 3), so skipping
i = 3, j = 6
- swapping
... etc
and you should end up with something like this
[1,2,3,4,5,2,3]
^ ^
i j
now you can cut the array at i, so you will get `[1,2,3,4,5,\0]` (in C syntax) ... so basically `[1,2,3,4,5]`
I have an array, and I'd like to go through it with a for loop in the following way:use 2 element then skip 2 elements,then use 2 elements etc.
So for instance if I have and int array : 1 2 3 4 5 6 7 8 9 10
I'd like to work with 1 and 2 then skip 3 and 4 ,then again work with 5,6 skip 7,8 etc.
Is this possible in java?
You can do something like this:
for (int i = 0; i < array.length - 1; i += 4) {
// work with i and (i + 1)
}
Yes, you need a loop, but two would be simpler.
for (int i = 0; i < N; i += 4)
for (int j = i; j < i + 1; j++)
System.out.println(j);
Also you can do
IntStream.range(0, N)
.flatMap(i -> IntStream.range(i+1, i+3))
.forEach(System.out::println):
Something along the lines of this pseudo code will do the job:
for (int i=0; i<array.length-3; i+=4) {
int first = array[i];
int second = array[i+1];
//int third = array[i+2]; //skip this
//int fourth = array[i+3]; //skip this
}
Note: normally when looping through an array you can just use i<array.length-1, but because you could be skipping 2 at that point I believe you want to decrease the "OK" length of the array by 2 more.
Update: actual working Java code using a toggle:
boolean skip = false;
for (int i=0; i<test.length-1; i+=2) {
if (!skip) {
System.out.println(test[i]);
System.out.println(test[i+1]);
}
//int third = array[i+2]; //skip this
//int fourth = array[i+3]; //skip this
skip = !skip;
}
You can introduce as many variables as you need inside of a for loop and iterate through those. It's similar to using two loops with the advantage being that there's no extra iterations being done.
The main thing here is that you're skipping four entries in your array every time. This applies to both variables, and you also want to be sure that both of them are less than the length of the overall array (so you don't step off of it by mistake).
for (int i = 0, j = i + 1; i < array.length && j < array.length; i += 4, j += 4) {
System.out.printf("(%d, %d) ", array[i], array[j]);
}
I don't know how to address the question properly to even google it so far.
In a nested loop such as this
for (int i=0;i>0;i++)
{
for(int k=0;k<0;k++)
{
}
}
What kind of applications would there be if we use k
I have this question because I wanted to make a loop which iterates like star printing with * char printing left triangle but it iterates on a 2 dimensional matrix as the cursor moves it iterates on the array items such as this
a[0][0]
a[1][0], a[1][1]
a[2][0], a[2][1], a[2][2]
a[3][0], a[3][1], a[3][2], a[3][3]
I want to figure out a for loop or something to be able to iterate the array such as this. What do you suggest?
You must change the first for condition, because when i = 0 the condition i > 0 is false, so it never enters the loop.
Note that when you go line be line, the k must iterate in this pattern: [0, 01, 012, 0123] while i in [0, 1, 2, 3]. In other words, k must iterate until it reaches the value of i, so the condition of the nested for must be k < i + 1.
for (int i = 0; i < 4; i++) {
for (int k = 0; k < i + 1; k++) {
// Here you should access to the array
// array[i][k]
System.out.print(i + " " + k + " - "); // [DEBUG]
}
System.out.println(); // [DEBUG]
}
Output: Just to see indexes
0 0 -
1 0 - 1 1 -
2 0 - 2 1 - 2 2 -
3 0 - 3 1 - 3 2 - 3 3 -
It looks like what you want is something like
for (int i = 0; i < SOME_LIMIT; ++i) {
for (int k = 0; k <= i; ++k) {
do_something_with (a[i][k]);
}
}
It looks like you've already done the hard part -- designing the basic premise of the application and writing down on paper what you'd like to do.
Now all you do is take what you've written down and translate it in to code.
I'll give you one hint. On the inside loop, for(int k=0;k<0;k++), thibnk about how you would change this in order to achieve the behavior you're looking for.
Keep at it, you'll get there. The hard part is already done.
Arrayindex out of bounds error is showing up and I have particularly no idea why it is hapening. I am trying create 2N tuple objects and trying to put them in a array of size 2N
Tuple[] minuteunit = new Tuple[2*N];
if(!intervals.isEmpty())
{
for(i = 0; i < ((2*N)-1); i = 1+2)
{
minuteunit[i] = new Tuple(intervals.get(i).getBeginMinuteUnit(),"s");
minuteunit[i+1] = new Tuple(intervals.get(i).getEndMinuteUnit(),"e");
}
it is most likely the face you are using i in intervals.get(i), because i is incrementing +2. I would imagine you have N values in intervals and therefore when i >= (N/2) you get an overflow.
Try this:
for(i = 0; i < N; i++)
{
minuteunit[2*i] = new Tuple(intervals.get(i).getBeginMinuteUnit(),"s");
minuteunit[2*i+1] = new Tuple(intervals.get(i).getEndMinuteUnit(),"e");
}
Also, assuming intervals should contain N entries, you could update your intervals.isEmpty() check to:
if(intevals.size() == N)
{
...
for(i = 0; i < ((2*N)-1); i = 1+2)
At each iteration i equals 3, so it never finishes. I think you want to add 2 at each iteration so use:
for(i = 0; i < ((2*N)-1); i = i+2)
You need to make your loop's termination condition i<2*N-2, otherwise the iteration of adding 2 will push i over the end of your array when i is 2*N-2 (which it wi eventually be).
btw, I assume that your iteration of i=1+2 is a typo, and you have actually coded i=i+2
I'm going to take a shot in the dark here and guess that maybe intervals.get(i) is what's actually causing your problem here and not the array itself. Ignoring what appears to be a typo in the increment, the condition for your loop seems to be fine, since if i < 2*N - 1, then i + 1 < 2*N and so there's no out of bound index for the array. That leaves the intervals.get(i) method.
First example
int windowStart = 0;
for (int i = 0; i + windowSize < fileArray.size(); i++) {
ArrayList <Character> window = new ArrayList <Character> ();
for (int s = windowStart; s <= windowStart + windowSize; s++) {
window.add(fileArray.get(s));
}
windowStart++;
}
VS.
second example
int ind = 0;
for (int i = 0; i + windowSize < fileArray.size(); i++) {
for (int b = ind; b <= windowSize + ind; b++) {
window.add(fileArray.get(b));
}
ind++;
}
The first one throws an java.lang.IndexOutOfBoundsException while the second one does not and works just fine. fileArray is the same for both, but for 2. the window array is defined as an attribute, while for the first one, the "window" array is defined inside the method (and the for loop). Does that make a difference?
You can't get an IndexOutOfBoundsException for adding a value to a list. The problem is that a value of s is equal or greater then the actual size of the fileArray array or list.
And because the loops in both examples are equivalent, the problem should be found outside the lines of code you've just posted.
Try to debug (break on IndexOutOfBoundsException if you are using an IDE, otherwise add some simple System.out.println statements to find out, why s is greater than expected.
From the first example, from what you're telling me:
windowSize = 30.
fileArray.size() = 235.
Then, your first for loop will terminate when i + windowSize > 235 (which means i = 236 - 30 = 206`.
You will realize that windowStart gets incremented inside the first for-loop, so eventually, windowStart will increment 205 times (starting from 0).
At that point, the 2nd for-loop will count (windowStart = 205) and your condition s <= windowStart + windowSize will be (s = 205 + 30 which equals 235), which satisfies the condition in the loop, and you're saying fileArray.get(s) which means, fileArray.get(235), and you're getting a IndexOutOfBoundsException.
I'm assuming that windowSize is not the same on the 2nd example, but I can't tell you that until I have seen the full source code.
Hope this helps.