A method or function that returns a list of pairs of numbers which would sum up to 5
I am trying to implement a function in JAVA which would give me a List of Pairs of Numbers.
This pair of numbers which would add up to 5, so ultimate goal is to print this list of pair of numbers
I have this code to determine pair but not sure how to return from method.
Please Note: I have used void as I have no clue of how to return values from method in the form (numberA[0], numberB[0]), (numberA[1], numberB[1]), ....?
public void setNumberList(int numberList)
ArrayList<Integer> numberA = new ArrayList<Integer>();
ArrayList<Integer> numberB = new ArrayList<Integer>();
for (int i = 0; i < numberList.length; i++)
int first = numberList[i];
for (int j = i + 1; j < numberList.length; j++)
int second = numberList[j];
if ((first + second) == 5)
numberA.add(first);
numberB.add(second);
java
|
show 2 more comments
I am trying to implement a function in JAVA which would give me a List of Pairs of Numbers.
This pair of numbers which would add up to 5, so ultimate goal is to print this list of pair of numbers
I have this code to determine pair but not sure how to return from method.
Please Note: I have used void as I have no clue of how to return values from method in the form (numberA[0], numberB[0]), (numberA[1], numberB[1]), ....?
public void setNumberList(int numberList)
ArrayList<Integer> numberA = new ArrayList<Integer>();
ArrayList<Integer> numberB = new ArrayList<Integer>();
for (int i = 0; i < numberList.length; i++)
int first = numberList[i];
for (int j = i + 1; j < numberList.length; j++)
int second = numberList[j];
if ((first + second) == 5)
numberA.add(first);
numberB.add(second);
java
2
Welcome to SO. Please post the code you already have and specify where you are having trouble. Stop by the help center and read How to Ask. Thanks.
– Johnny Mopp
Nov 13 '18 at 18:27
I'm guessing there must also be other limits, otherwise this question has an infinite number of solutions.-23 + 28
, as just one example.
– KevinO
Nov 13 '18 at 18:31
You have the numbers in your listsnumberA
andnumberB
. Please edit your question with the problem you have right now, you have not asked any question.
– Progman
Nov 13 '18 at 19:27
how to return values from method in the form (numberA[0], numberB[0]), (numberA[1], numberB[1]), ....
– BVK 0118
Nov 13 '18 at 19:30
@BVK0118 You can change the return type toSet<List<Integer>>
. And return aSet
with lists of numbers (which will have only 2 elements each) which sum results in your search number5
.
– Progman
Nov 13 '18 at 19:36
|
show 2 more comments
I am trying to implement a function in JAVA which would give me a List of Pairs of Numbers.
This pair of numbers which would add up to 5, so ultimate goal is to print this list of pair of numbers
I have this code to determine pair but not sure how to return from method.
Please Note: I have used void as I have no clue of how to return values from method in the form (numberA[0], numberB[0]), (numberA[1], numberB[1]), ....?
public void setNumberList(int numberList)
ArrayList<Integer> numberA = new ArrayList<Integer>();
ArrayList<Integer> numberB = new ArrayList<Integer>();
for (int i = 0; i < numberList.length; i++)
int first = numberList[i];
for (int j = i + 1; j < numberList.length; j++)
int second = numberList[j];
if ((first + second) == 5)
numberA.add(first);
numberB.add(second);
java
I am trying to implement a function in JAVA which would give me a List of Pairs of Numbers.
This pair of numbers which would add up to 5, so ultimate goal is to print this list of pair of numbers
I have this code to determine pair but not sure how to return from method.
Please Note: I have used void as I have no clue of how to return values from method in the form (numberA[0], numberB[0]), (numberA[1], numberB[1]), ....?
public void setNumberList(int numberList)
ArrayList<Integer> numberA = new ArrayList<Integer>();
ArrayList<Integer> numberB = new ArrayList<Integer>();
for (int i = 0; i < numberList.length; i++)
int first = numberList[i];
for (int j = i + 1; j < numberList.length; j++)
int second = numberList[j];
if ((first + second) == 5)
numberA.add(first);
numberB.add(second);
java
java
edited Nov 13 '18 at 19:31
BVK 0118
asked Nov 13 '18 at 18:24
BVK 0118BVK 0118
43
43
2
Welcome to SO. Please post the code you already have and specify where you are having trouble. Stop by the help center and read How to Ask. Thanks.
– Johnny Mopp
Nov 13 '18 at 18:27
I'm guessing there must also be other limits, otherwise this question has an infinite number of solutions.-23 + 28
, as just one example.
– KevinO
Nov 13 '18 at 18:31
You have the numbers in your listsnumberA
andnumberB
. Please edit your question with the problem you have right now, you have not asked any question.
– Progman
Nov 13 '18 at 19:27
how to return values from method in the form (numberA[0], numberB[0]), (numberA[1], numberB[1]), ....
– BVK 0118
Nov 13 '18 at 19:30
@BVK0118 You can change the return type toSet<List<Integer>>
. And return aSet
with lists of numbers (which will have only 2 elements each) which sum results in your search number5
.
– Progman
Nov 13 '18 at 19:36
|
show 2 more comments
2
Welcome to SO. Please post the code you already have and specify where you are having trouble. Stop by the help center and read How to Ask. Thanks.
– Johnny Mopp
Nov 13 '18 at 18:27
I'm guessing there must also be other limits, otherwise this question has an infinite number of solutions.-23 + 28
, as just one example.
– KevinO
Nov 13 '18 at 18:31
You have the numbers in your listsnumberA
andnumberB
. Please edit your question with the problem you have right now, you have not asked any question.
– Progman
Nov 13 '18 at 19:27
how to return values from method in the form (numberA[0], numberB[0]), (numberA[1], numberB[1]), ....
– BVK 0118
Nov 13 '18 at 19:30
@BVK0118 You can change the return type toSet<List<Integer>>
. And return aSet
with lists of numbers (which will have only 2 elements each) which sum results in your search number5
.
– Progman
Nov 13 '18 at 19:36
2
2
Welcome to SO. Please post the code you already have and specify where you are having trouble. Stop by the help center and read How to Ask. Thanks.
– Johnny Mopp
Nov 13 '18 at 18:27
Welcome to SO. Please post the code you already have and specify where you are having trouble. Stop by the help center and read How to Ask. Thanks.
– Johnny Mopp
Nov 13 '18 at 18:27
I'm guessing there must also be other limits, otherwise this question has an infinite number of solutions.
-23 + 28
, as just one example.– KevinO
Nov 13 '18 at 18:31
I'm guessing there must also be other limits, otherwise this question has an infinite number of solutions.
-23 + 28
, as just one example.– KevinO
Nov 13 '18 at 18:31
You have the numbers in your lists
numberA
and numberB
. Please edit your question with the problem you have right now, you have not asked any question.– Progman
Nov 13 '18 at 19:27
You have the numbers in your lists
numberA
and numberB
. Please edit your question with the problem you have right now, you have not asked any question.– Progman
Nov 13 '18 at 19:27
how to return values from method in the form (numberA[0], numberB[0]), (numberA[1], numberB[1]), ....
– BVK 0118
Nov 13 '18 at 19:30
how to return values from method in the form (numberA[0], numberB[0]), (numberA[1], numberB[1]), ....
– BVK 0118
Nov 13 '18 at 19:30
@BVK0118 You can change the return type to
Set<List<Integer>>
. And return a Set
with lists of numbers (which will have only 2 elements each) which sum results in your search number 5
.– Progman
Nov 13 '18 at 19:36
@BVK0118 You can change the return type to
Set<List<Integer>>
. And return a Set
with lists of numbers (which will have only 2 elements each) which sum results in your search number 5
.– Progman
Nov 13 '18 at 19:36
|
show 2 more comments
1 Answer
1
active
oldest
votes
You can change the return type of your method to Set<List<Integer>>
. The Set
will contain lists of numbers, where the numbers in each list will sum up to your target number 5
. The method will look like this:
public static Set<List<Integer>> setNumberList(int numberList)
ArrayList<Integer> numberA = new ArrayList<Integer>();
ArrayList<Integer> numberB = new ArrayList<Integer>();
for (int i = 0; i < numberList.length; i++)
int first = numberList[i];
for (int j = i + 1; j < numberList.length; j++)
int second = numberList[j];
if ((first + second) == 5)
numberA.add(first);
numberB.add(second);
Set<List<Integer>> result = new HashSet<List<Integer>>();
for (int i=0; i<numberA.size(); i++)
int n1 = numberA.get(i);
int n2 = numberB.get(i);
List<Integer> numbersToAdd = Arrays.asList(n1, n2);
result.add(numbersToAdd);
return result;
Thank you so much for your help! It works!
– BVK 0118
Nov 14 '18 at 22:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287325%2fa-method-or-function-that-returns-a-list-of-pairs-of-numbers-which-would-sum-up%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can change the return type of your method to Set<List<Integer>>
. The Set
will contain lists of numbers, where the numbers in each list will sum up to your target number 5
. The method will look like this:
public static Set<List<Integer>> setNumberList(int numberList)
ArrayList<Integer> numberA = new ArrayList<Integer>();
ArrayList<Integer> numberB = new ArrayList<Integer>();
for (int i = 0; i < numberList.length; i++)
int first = numberList[i];
for (int j = i + 1; j < numberList.length; j++)
int second = numberList[j];
if ((first + second) == 5)
numberA.add(first);
numberB.add(second);
Set<List<Integer>> result = new HashSet<List<Integer>>();
for (int i=0; i<numberA.size(); i++)
int n1 = numberA.get(i);
int n2 = numberB.get(i);
List<Integer> numbersToAdd = Arrays.asList(n1, n2);
result.add(numbersToAdd);
return result;
Thank you so much for your help! It works!
– BVK 0118
Nov 14 '18 at 22:08
add a comment |
You can change the return type of your method to Set<List<Integer>>
. The Set
will contain lists of numbers, where the numbers in each list will sum up to your target number 5
. The method will look like this:
public static Set<List<Integer>> setNumberList(int numberList)
ArrayList<Integer> numberA = new ArrayList<Integer>();
ArrayList<Integer> numberB = new ArrayList<Integer>();
for (int i = 0; i < numberList.length; i++)
int first = numberList[i];
for (int j = i + 1; j < numberList.length; j++)
int second = numberList[j];
if ((first + second) == 5)
numberA.add(first);
numberB.add(second);
Set<List<Integer>> result = new HashSet<List<Integer>>();
for (int i=0; i<numberA.size(); i++)
int n1 = numberA.get(i);
int n2 = numberB.get(i);
List<Integer> numbersToAdd = Arrays.asList(n1, n2);
result.add(numbersToAdd);
return result;
Thank you so much for your help! It works!
– BVK 0118
Nov 14 '18 at 22:08
add a comment |
You can change the return type of your method to Set<List<Integer>>
. The Set
will contain lists of numbers, where the numbers in each list will sum up to your target number 5
. The method will look like this:
public static Set<List<Integer>> setNumberList(int numberList)
ArrayList<Integer> numberA = new ArrayList<Integer>();
ArrayList<Integer> numberB = new ArrayList<Integer>();
for (int i = 0; i < numberList.length; i++)
int first = numberList[i];
for (int j = i + 1; j < numberList.length; j++)
int second = numberList[j];
if ((first + second) == 5)
numberA.add(first);
numberB.add(second);
Set<List<Integer>> result = new HashSet<List<Integer>>();
for (int i=0; i<numberA.size(); i++)
int n1 = numberA.get(i);
int n2 = numberB.get(i);
List<Integer> numbersToAdd = Arrays.asList(n1, n2);
result.add(numbersToAdd);
return result;
You can change the return type of your method to Set<List<Integer>>
. The Set
will contain lists of numbers, where the numbers in each list will sum up to your target number 5
. The method will look like this:
public static Set<List<Integer>> setNumberList(int numberList)
ArrayList<Integer> numberA = new ArrayList<Integer>();
ArrayList<Integer> numberB = new ArrayList<Integer>();
for (int i = 0; i < numberList.length; i++)
int first = numberList[i];
for (int j = i + 1; j < numberList.length; j++)
int second = numberList[j];
if ((first + second) == 5)
numberA.add(first);
numberB.add(second);
Set<List<Integer>> result = new HashSet<List<Integer>>();
for (int i=0; i<numberA.size(); i++)
int n1 = numberA.get(i);
int n2 = numberB.get(i);
List<Integer> numbersToAdd = Arrays.asList(n1, n2);
result.add(numbersToAdd);
return result;
answered Nov 14 '18 at 9:55
ProgmanProgman
6,35331936
6,35331936
Thank you so much for your help! It works!
– BVK 0118
Nov 14 '18 at 22:08
add a comment |
Thank you so much for your help! It works!
– BVK 0118
Nov 14 '18 at 22:08
Thank you so much for your help! It works!
– BVK 0118
Nov 14 '18 at 22:08
Thank you so much for your help! It works!
– BVK 0118
Nov 14 '18 at 22:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287325%2fa-method-or-function-that-returns-a-list-of-pairs-of-numbers-which-would-sum-up%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Welcome to SO. Please post the code you already have and specify where you are having trouble. Stop by the help center and read How to Ask. Thanks.
– Johnny Mopp
Nov 13 '18 at 18:27
I'm guessing there must also be other limits, otherwise this question has an infinite number of solutions.
-23 + 28
, as just one example.– KevinO
Nov 13 '18 at 18:31
You have the numbers in your lists
numberA
andnumberB
. Please edit your question with the problem you have right now, you have not asked any question.– Progman
Nov 13 '18 at 19:27
how to return values from method in the form (numberA[0], numberB[0]), (numberA[1], numberB[1]), ....
– BVK 0118
Nov 13 '18 at 19:30
@BVK0118 You can change the return type to
Set<List<Integer>>
. And return aSet
with lists of numbers (which will have only 2 elements each) which sum results in your search number5
.– Progman
Nov 13 '18 at 19:36