• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with index removal  RSS feed

 
Dale Herring
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am trying to create a method that removes an index from an array and copies the rest into a new array.
Here is the code pertinent to what I am trying to do.


I have tried different approaches but am not sure if I am heading in the right direction. I am also getting an out of bounds error when I try to copy the array alone which I am assuming is an off-by-one that I am not seeing

 
Knute Snortum
Sheriff
Posts: 4079
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your out of bounds error happens here (last line):

retData3 length is one less than data3. That means it is four long. for(i=0;i<5;i++) will iterate five times (0-4).
 
Knute Snortum
Sheriff
Posts: 4079
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several thing wrong with the following method. I have formatted it properly. Notice how good formatting will show logic errors easily.

You have idx as a parameter but then you assign it 2. You should use the parameter value.

You always assign dat3[i] = data3[i]. This doesn't make sense it you're trying to remove an index.

The two if's in the for loop are very muddled. First you check if the value of dat3[i] is 2. Why 2? Then, if it's true, you set dat3[2] = 0. Why is the index 2? And setting a value to zero doesn't remove the index. Then after setting dat3[2] = 0, you test if dat3[2] == 0. But you know it's 0; you just set it. If true, you continue. But there's no code that gets skipped if dat3[2] != 0. The continue does nothing.

The return statement is inside braces. There's no need for that.
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to what Knute said, there are some other problems in your code.

What is the purpose of remove() having idx as a parameter when you simply set it to 2 in the second line of the method (i.e. you discard the value passed in without ever using it)?

Furthermore, what's the point of setting idx to 2 in the method if you don't use idx and hardcode in 2 e.g. if(dat3[i] == 2)? Surely this should be dat3[i]==idx), right?
 
Dale Herring
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. I may have missed putting a line in there because of me trying to debug and switch things around. idx is the index of what I want to remove from the array. Should be something like dat3[idx]. I am trying to copy the array except for the value at idx, which is just a value I've chosen. The out of bounds should be taken care of once the new array is returned. I am just not sure how to go about removing it. I want to do something akin to the opposite of this...

 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, if I understand correctly, if you have myArray = {1, 2, 3, 4, 5, 6} and you call remove(myArray, 4), then you should get newArray = {1, 2, 3, 5, 6}. Fred clarified below what you are trying to do.

If so, the code below will do this.



Things to note:
1) You do not set the value of idx in the remove method, instead you pass the value in when you call remove().
2) You have to use data3.length in the for loop, and not data3.length-1 as you used in post #1.
3) You need two counters - one for the original array, and one for the new array. You only increment the value of the counter for new array if you insert something in. This is represented by dat3_pos++. If you do not insert anything in (i.e. when you hit the index value you want to remove from the original array), you do not increment the value of the counter of the new array (as you didn't insert anything in).
 
Knute Snortum
Sheriff
Posts: 4079
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't post complete solutions in this forum. This program could be a homework assignment. Even if not, we want to guide the OP to the solution. This provides a better learning experience.
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Please don't post complete solutions in this forum. This program could be a homework assignment. Even if not, we want to guide the OP to the solution. This provides a better learning experience.


I am not sure what I have done wrong here.

I didn't post the solution in my first response, when it became apparent that this problem is only part of the "assignment" this person is doing (i.e. you can see they also need to implement an insert method), I decided to do it for him as it's just a few lines of code, and sometimes it's just easier to learn by looking at the solution in front of you and see how it is done. The OP had clearly made an effort but was still struggling and I felt it might be more productive for him if he had a solution and could learn from that.
 
Junilu Lacar
Sheriff
Posts: 11164
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point Knute is trying to make, Ahmed, is that if it's schoolwork, we want to avoid putting OP in a compromised position with respect to policies his school may have regarding getting external assistance. See the policy stated in this document for a course that one of our other forum participants recently posted.

So, despite all your best intentions, and I admit, we all have at some point probably have pushed the edge with this policy, we still discourage giving out your solution to their homework, no matter how much effort they have put in and no matter how much we feel that handing them a gimme will help them in the long run. If anything, we would probably be on the safe side if we wait a while so that any deadlines they may have for submitting their work has passed. That way, getting help truly becomes more a matter of learning than it is a matter of making the grade. Bottom line, we want to err on the side of caution and not be party, wittingly or unwittingly, to anything that could get OP in trouble academically or ethically.

Does that make sense?
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it makes perfect sense, I had not considered that angle. Thanks for clarifying!
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahmed Bin S wrote:Ok, if I understand correctly, if you have myArray = {1, 2, 3, 4, 5, 6} and you call remove(myArray, 4), then you should get newArray = {1, 2, 3, 5, 6}.

I don't think this is correct. I assumed that the second argument to remove() was the index of the item to remove. So the answer should be {1, 2, 3, 4, 6}. That is, remove "5" since it is the value at array index 4.
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:
I don't think this is correct. I assumed that the second argument to remove() was the index of the item to remove. So the answer should be {1, 2, 3, 4, 6}. That is, remove "5" since it is the value at array index 4.


You are indeed correct, I have amended my post and made a note of this.

Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!