• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Add found indices to an array

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to implement a method that takes an array of numbers (integers for the tests) and a target number. It should find two different items in the array that, when added together, give the target value. The indices of these items should then be returned in an array like so: [index1, index2].
The input will always be valid (numbers will be an array of length 2 or greater, and all of the items will be numbers; target will always be the sum of two different items from that array).
My work so far:

The method works, I manage to get the two required indexes, but I can't find a way to add them to the pair array (lines 21-22 do not work as I expected them to).
Can someone help, please?
Thanks
 
Marshal
Posts: 64707
226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain what the pair array is supposed to contain. I don't think it contains what you think it does.
 
Sabrina Cux
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please explain what the pair array is supposed to contain. I don't think it contains what you think it does.


The pair should contain the 2 indexes of the two different items of the array passed in the method that, when added together, give the target value.
 
Rancher
Posts: 4122
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And on lines 19 and 20 what values are you putting in that array?
 
Sabrina Cux
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:And on lines 19 and 20 what values are you putting in that array?


The two supposed found indexes if(sum == target) then I should have found the indexes, and I need to add them to the array returned by the method as requested.
 
Dave Tolls
Rancher
Posts: 4122
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you're setting them to numbers[left] and numbers[right].
What are those values?
 
Sabrina Cux
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:But you're setting them to numbers[left] and numbers[right].
What are those values?


The values are the index values. If you remove the comments on lines 21 22 you can see they get printed. How to have them inside pair? I am probably confusing something here...
 
Dave Tolls
Rancher
Posts: 4122
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite.
They're the values, not the indexes.

You said that pair should contain the two indexes of the entries in the array that add up to the given number.
 
Sabrina Cux
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I got my misunderstanding, if I type I get printed just the indexes (and not their values), but I don't need them printed, I need them inside the pair array. Is there any way to achieve that, without changing the whole code?
 
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just change "numbers[left]" to "left" on Line 19,  "numbers[right]" to "right" on Line 20, analogous to the changes on Lines 21 and 22?

PS - A minor comment: You don't actually need to make an object of the class on which to call the method, as it is static, i.e Lines 5 and 6 can be replaced by
 
Elaine Byrne
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going to suggest then referencing the array returned from the method call and printing the indices that have been put in the 2-element array:

but I realise see the numbers are 0 and 1, rather than the 0 and 2 that I would have expected. (Same as printed from within the method). So I'm not sure if the code is doing what you want. I haven't thought too much about the logic, as I got a bit lost on initial think-through)
 
Saloon Keeper
Posts: 3304
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sabrina,

a question: must you return two different indices or two different values?
You can simplify your code by using a suitable version of Arrays.binarySearch.
 
Sabrina Cux
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

E Byrne wrote:I was going to suggest then referencing the array returned from the method call and printing the indices that have been put in the 2-element array:

but I realise see the numbers are 0 and 1, rather than the 0 and 2 that I would have expected. (Same as printed from within the method). So I'm not sure if the code is doing what you want. I haven't thought too much about the logic, as I got a bit lost on initial think-through)


Yes, I noticed that too. But I soon found out that was because of on line 10, I removed it and it's working fine now.
 
Sabrina Cux
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Sabrina,

a question: must you return two different indices or two different values?
You can simplify your code by using a suitable version of Arrays.binarySearch.


I need the two differente indexed (whose summed values are equal to the target).
 
Piet Souris
Saloon Keeper
Posts: 3304
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code of your opening post, in line 15, you have:

that's why I asked.

For instance, if the array = {4, 4, 4, 4} and the required sum = 8, what would you return?
 
Sabrina Cux
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

E Byrne wrote:Just change "numbers[left]" to "left" on Line 19,  "numbers[right]" to "right" on Line 20, analogous to the changes on Lines 21 and 22?


I tried that but, in both cases (pair[0]=numbers[right] or just right) it gives me back a dirty array, like This is my issue.
 
Sabrina Cux
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:In the code of your opening post, in line 15, you have:

that's why I asked.

For instance, if the array = {4, 4, 4, 4} and the required sum = 8, what would you return?


"Target will always be the sum of two different items from that array." This is just an exercise to practice.  I guessed it means two different values, as they can't have same indexes.
 
Dave Tolls
Rancher
Posts: 4122
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sabrina Cux wrote:
I tried that but, in both cases (pair[0]=numbers[right] or just right) it gives me back a dirty array, like This is my issue.



That's because that's the default output from an array.
If you want the contents then use:
System.out.println(Arrays.toString(pair));
 
Elaine Byrne
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sabrina Cux wrote:

E Byrne wrote:I was going to suggest then referencing the array returned from the method call and printing the indices that have been put in the 2-element array:

but I realise see the numbers are 0 and 1, rather than the 0 and 2 that I would have expected. (Same as printed from within the method). So I'm not sure if the code is doing what you want. I haven't thought too much about the logic, as I got a bit lost on initial think-through)


Yes, I noticed that too. But I soon found out that was because of on line 10, I removed it and it's working fine now.


Ah, yes - I should have noticed that myself!
 
Elaine Byrne
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sabrina Cux wrote:

E Byrne wrote:Just change "numbers[left]" to "left" on Line 19,  "numbers[right]" to "right" on Line 20, analogous to the changes on Lines 21 and 22?


I tried that but, in both cases (pair[0]=numbers[right] or just right) it gives me back a dirty array, like This is my issue.


As Dave implies, this would be the result of printing an array object, i.e. and I don't know where you're doing that. Maybe you could post the latest version of your code? What I meant was that lines 19 and 20 would be:                    
 
Sabrina Cux
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Sabrina Cux wrote:
I tried that but, in both cases (pair[0]=numbers[right] or just right) it gives me back a dirty array, like This is my issue.



That's because that's the default output from an array.
If you want the contents then use:
System.out.println(Arrays.toString(pair));


Thank you so much!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!