• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to return a sequence pair of array in Java?  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I return a sequence of array pair for example:



desired output: [[34,23], [34, 35], [34, 43], [23, 45], [23, 43], [45, 43]]

my approach (looking for a possible loop)


My next initial approach is to put firstElement and newFirstElement into an array so like:
add into some array (multidimensional) = [[34, 23]]

 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I don't think removing elements from the list will help you. Your best bet is a couple of for loops, one nested in the other. Walk through the process using pen and paper to note down the indices of the elements you're pairing up. you should be able to see a pattern after a few times through the iterations.

Good luck.
 
mike ashlon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I came up with on paper which I tried to implement but with the wrong output:

for arraylist = {0,1,2,3,4}

Loop 1 i = 0
> int firstindex = array.get(0)
> arraylist.remove(firstindex);
> output = [0 , 1], [0 , 2], [0 , 3], [0 , 4]
Loop 2 i = 1
> int firstindex = array.get(0)
> arraylist.remove(firstindex);
> output = [1 , 2], [1 , 3], [1 , 4]
Loop 3 i = 2
> int firstindex = array.get(0)
> arraylist.remove(firstindex);
> output = [2 , 3], [2 , 4]
Loop 4 i = 3
> int firstindex = array.get(0)
> arraylist.remove(firstindex);
> output = [3 , 4]

I don't understand the part where the loop omits the previous indexs which I tried to implement by removing (clearly not working for me)
 
mike ashlon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at your outcomes:

[0,1], [0,2] , ...
[1, 2], [1, 3], ..
[2,3], [2,4]...

and the first number is your i, and the second is your j, what can you say about the starting value of j in your loop? Is that always 1?
 
Marshal
Posts: 56606
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Please always use the code button; I shall try editing your posts and you will see how much better it looks You appear to have got that bit right. Sorry

Please be specific about what you are trying to do. The thread title says, “return a sequence,” and you are printing pairs. The two are different. I would interpret returning a sequence of pairs as meaning you have an IntPair class and you are adding instances of it to a List which you later return.
Junilu is right that there is no need to remove anything. And Piet is right about counting the indices. I can see a simpler way to display the pairs which does not need a second loop. It is however dependent on the List containing an even number of elements. That will allow you to display [1,2] [3,4] [5,6] [7,8] etc. At least I think that is what you want.
If you want to display [1,2] [1,3] [1,4]...[2,3] [2,4] etc., then you will want two loops.

Piet is right about looking at your outcomes. Until you can visualise the algorithm in the back of your head, you will find things much easier to work out with a (very soft) pencil and paper. You will also need an eraser to destroy the evidence. A dirty great big eraser because there will probably be lots of evidence to destroy
 
mike ashlon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that j is instantiated incorrectly with the wrong value, how do I set it to the next index accordingly to each round of the loop? or what should it be set to
 
mike ashlon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have it,



Although how do I add the final output into array so it returns
[[0,1][0,2][0,3][0,4][1,2][1,3][1,4][2,3][2,4][3,4]] rather than [0,1][0,2][0,3][0,4][1,2][1,3][1,4][2,3][2,4][3,4]

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It probably escapes you because you don't know the idiom.  If you wrote down everything that happens through the iterations of i and j, you'll see this:


Look at how the value of i changes. Look at where j starts every time i changes. There's a consistent pattern there.  You have to define the starting value of j relative to the current value of i.  You figure out the math.
 
Campbell Ritchie
Marshal
Posts: 56606
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike ashlon wrote:. . .. . .
What is line 2 supposed to mean? What if your List contains 99? Are you going to look for element 99 (which is, by the way, the 100th position in the List)? Has it got a 100th element? Won't the runtime have things to say if not? (Like index out of bounds exception?) That means start at the element whose number is the same as the 2nd element in the List. Sorry, I think I am getting carried away a bit.
I think you want to set j to something different, something much simpler. I also think your something simpler will make line 3 unnecessary.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!