How to print out results in NQueens
up vote
1
down vote
favorite
I am new to JAVA. Below is my code of NQueens problem. The results are [[Ljava.lang.String;@123a439b, [Ljava.lang.String;@7de26db8].
Can anyone please help? Thank you very much! I copyied the code from other's blog. The code should be correct. I just don't know how to print out the results. I think this should be not very hard. My background is not computer science, that's maybe why I have the trouble. Thank you!
import java.util.ArrayList;
public class Solution {
public ArrayList<String> solveNQueens(int n) {
ArrayList<String> res = new ArrayList<String>();
if(n<=0)
return res;
int columnVal = new int[n];
DFS_helper(n,res,0,columnVal);
return res;
}
public void DFS_helper(int nQueens, ArrayList<String> res, int row, int columnVal){
if(row == nQueens){
String unit = new String[nQueens];
for(int i = 0; i < nQueens; i++){
StringBuilder s = new StringBuilder();
for(int j = 0; j < nQueens; j++){
if(j == columnVal[i])
s.append("Q ");
else
s.append("+ ");
}
unit[i] = s.toString();
//System.out.println(unit[i]);
}
//System.out.println();
res.add(unit);
// System.out.println(Arrays.toString(res));
//return;
}
else{
for(int i = 0; i < nQueens; i++){
columnVal[row] = i;//(row,columnVal[row)==>(row,i)
if(isValid(row,columnVal))
DFS_helper(nQueens, res, row+1, columnVal);
}
}
}
public boolean isValid(int row, int columnVal){
for(int i = 0; i < row; i++){
if(columnVal[row] == columnVal[i]
||Math.abs(columnVal[row]-columnVal[i]) == row-i)
return false;
}
return true;
}
public static void main(String args) {
Solution su = new Solution();
int n = 4;
System.out.println(su.solveNQueens(n));
}
}
java
add a comment |
up vote
1
down vote
favorite
I am new to JAVA. Below is my code of NQueens problem. The results are [[Ljava.lang.String;@123a439b, [Ljava.lang.String;@7de26db8].
Can anyone please help? Thank you very much! I copyied the code from other's blog. The code should be correct. I just don't know how to print out the results. I think this should be not very hard. My background is not computer science, that's maybe why I have the trouble. Thank you!
import java.util.ArrayList;
public class Solution {
public ArrayList<String> solveNQueens(int n) {
ArrayList<String> res = new ArrayList<String>();
if(n<=0)
return res;
int columnVal = new int[n];
DFS_helper(n,res,0,columnVal);
return res;
}
public void DFS_helper(int nQueens, ArrayList<String> res, int row, int columnVal){
if(row == nQueens){
String unit = new String[nQueens];
for(int i = 0; i < nQueens; i++){
StringBuilder s = new StringBuilder();
for(int j = 0; j < nQueens; j++){
if(j == columnVal[i])
s.append("Q ");
else
s.append("+ ");
}
unit[i] = s.toString();
//System.out.println(unit[i]);
}
//System.out.println();
res.add(unit);
// System.out.println(Arrays.toString(res));
//return;
}
else{
for(int i = 0; i < nQueens; i++){
columnVal[row] = i;//(row,columnVal[row)==>(row,i)
if(isValid(row,columnVal))
DFS_helper(nQueens, res, row+1, columnVal);
}
}
}
public boolean isValid(int row, int columnVal){
for(int i = 0; i < row; i++){
if(columnVal[row] == columnVal[i]
||Math.abs(columnVal[row]-columnVal[i]) == row-i)
return false;
}
return true;
}
public static void main(String args) {
Solution su = new Solution();
int n = 4;
System.out.println(su.solveNQueens(n));
}
}
java
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am new to JAVA. Below is my code of NQueens problem. The results are [[Ljava.lang.String;@123a439b, [Ljava.lang.String;@7de26db8].
Can anyone please help? Thank you very much! I copyied the code from other's blog. The code should be correct. I just don't know how to print out the results. I think this should be not very hard. My background is not computer science, that's maybe why I have the trouble. Thank you!
import java.util.ArrayList;
public class Solution {
public ArrayList<String> solveNQueens(int n) {
ArrayList<String> res = new ArrayList<String>();
if(n<=0)
return res;
int columnVal = new int[n];
DFS_helper(n,res,0,columnVal);
return res;
}
public void DFS_helper(int nQueens, ArrayList<String> res, int row, int columnVal){
if(row == nQueens){
String unit = new String[nQueens];
for(int i = 0; i < nQueens; i++){
StringBuilder s = new StringBuilder();
for(int j = 0; j < nQueens; j++){
if(j == columnVal[i])
s.append("Q ");
else
s.append("+ ");
}
unit[i] = s.toString();
//System.out.println(unit[i]);
}
//System.out.println();
res.add(unit);
// System.out.println(Arrays.toString(res));
//return;
}
else{
for(int i = 0; i < nQueens; i++){
columnVal[row] = i;//(row,columnVal[row)==>(row,i)
if(isValid(row,columnVal))
DFS_helper(nQueens, res, row+1, columnVal);
}
}
}
public boolean isValid(int row, int columnVal){
for(int i = 0; i < row; i++){
if(columnVal[row] == columnVal[i]
||Math.abs(columnVal[row]-columnVal[i]) == row-i)
return false;
}
return true;
}
public static void main(String args) {
Solution su = new Solution();
int n = 4;
System.out.println(su.solveNQueens(n));
}
}
java
I am new to JAVA. Below is my code of NQueens problem. The results are [[Ljava.lang.String;@123a439b, [Ljava.lang.String;@7de26db8].
Can anyone please help? Thank you very much! I copyied the code from other's blog. The code should be correct. I just don't know how to print out the results. I think this should be not very hard. My background is not computer science, that's maybe why I have the trouble. Thank you!
import java.util.ArrayList;
public class Solution {
public ArrayList<String> solveNQueens(int n) {
ArrayList<String> res = new ArrayList<String>();
if(n<=0)
return res;
int columnVal = new int[n];
DFS_helper(n,res,0,columnVal);
return res;
}
public void DFS_helper(int nQueens, ArrayList<String> res, int row, int columnVal){
if(row == nQueens){
String unit = new String[nQueens];
for(int i = 0; i < nQueens; i++){
StringBuilder s = new StringBuilder();
for(int j = 0; j < nQueens; j++){
if(j == columnVal[i])
s.append("Q ");
else
s.append("+ ");
}
unit[i] = s.toString();
//System.out.println(unit[i]);
}
//System.out.println();
res.add(unit);
// System.out.println(Arrays.toString(res));
//return;
}
else{
for(int i = 0; i < nQueens; i++){
columnVal[row] = i;//(row,columnVal[row)==>(row,i)
if(isValid(row,columnVal))
DFS_helper(nQueens, res, row+1, columnVal);
}
}
}
public boolean isValid(int row, int columnVal){
for(int i = 0; i < row; i++){
if(columnVal[row] == columnVal[i]
||Math.abs(columnVal[row]-columnVal[i]) == row-i)
return false;
}
return true;
}
public static void main(String args) {
Solution su = new Solution();
int n = 4;
System.out.println(su.solveNQueens(n));
}
}
java
java
asked Nov 20 at 3:54
Xinya Wang
26
26
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
While you do System.out.println(su.solveNQueens(n));
it prints list contents which are array and it just prints array object, but not it's contents. so, to print array
contents you need to iterate through them.
Arrays.toString Returns a string representation of the contents of the specified array.
https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object)
get result into a list
and then print it:
List<String> res = su.solveNQueens(n);
for(String strs : res) {
System.out.println(Arrays.toString(strs));
}
You can do like this too as the above prints with ,
. Here iterating through the list and then array.
List<String> res = su.solveNQueens(n);
for(String strs : res) {
for(String s: strs) {
System.out.print(s+ " ");
}
System.out.println();
}
in java 8, you can make use of stream
and lambda
:
ArrayList<String> result = su.solveNQueens(n)
result.stream().forEach(i->System.out.println(Arrays.toString(i)));
It works. Thank you. Can you please explain more? I am new to java. Thanks!
– Xinya Wang
Nov 20 at 4:01
Yes. Thank you very much!
– Xinya Wang
Nov 20 at 4:05
You explanation helps me a lot! Thank you very very much! ^_^
– Xinya Wang
Nov 20 at 4:17
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%2f53385985%2fhow-to-print-out-results-in-nqueens%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
up vote
1
down vote
While you do System.out.println(su.solveNQueens(n));
it prints list contents which are array and it just prints array object, but not it's contents. so, to print array
contents you need to iterate through them.
Arrays.toString Returns a string representation of the contents of the specified array.
https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object)
get result into a list
and then print it:
List<String> res = su.solveNQueens(n);
for(String strs : res) {
System.out.println(Arrays.toString(strs));
}
You can do like this too as the above prints with ,
. Here iterating through the list and then array.
List<String> res = su.solveNQueens(n);
for(String strs : res) {
for(String s: strs) {
System.out.print(s+ " ");
}
System.out.println();
}
in java 8, you can make use of stream
and lambda
:
ArrayList<String> result = su.solveNQueens(n)
result.stream().forEach(i->System.out.println(Arrays.toString(i)));
It works. Thank you. Can you please explain more? I am new to java. Thanks!
– Xinya Wang
Nov 20 at 4:01
Yes. Thank you very much!
– Xinya Wang
Nov 20 at 4:05
You explanation helps me a lot! Thank you very very much! ^_^
– Xinya Wang
Nov 20 at 4:17
add a comment |
up vote
1
down vote
While you do System.out.println(su.solveNQueens(n));
it prints list contents which are array and it just prints array object, but not it's contents. so, to print array
contents you need to iterate through them.
Arrays.toString Returns a string representation of the contents of the specified array.
https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object)
get result into a list
and then print it:
List<String> res = su.solveNQueens(n);
for(String strs : res) {
System.out.println(Arrays.toString(strs));
}
You can do like this too as the above prints with ,
. Here iterating through the list and then array.
List<String> res = su.solveNQueens(n);
for(String strs : res) {
for(String s: strs) {
System.out.print(s+ " ");
}
System.out.println();
}
in java 8, you can make use of stream
and lambda
:
ArrayList<String> result = su.solveNQueens(n)
result.stream().forEach(i->System.out.println(Arrays.toString(i)));
It works. Thank you. Can you please explain more? I am new to java. Thanks!
– Xinya Wang
Nov 20 at 4:01
Yes. Thank you very much!
– Xinya Wang
Nov 20 at 4:05
You explanation helps me a lot! Thank you very very much! ^_^
– Xinya Wang
Nov 20 at 4:17
add a comment |
up vote
1
down vote
up vote
1
down vote
While you do System.out.println(su.solveNQueens(n));
it prints list contents which are array and it just prints array object, but not it's contents. so, to print array
contents you need to iterate through them.
Arrays.toString Returns a string representation of the contents of the specified array.
https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object)
get result into a list
and then print it:
List<String> res = su.solveNQueens(n);
for(String strs : res) {
System.out.println(Arrays.toString(strs));
}
You can do like this too as the above prints with ,
. Here iterating through the list and then array.
List<String> res = su.solveNQueens(n);
for(String strs : res) {
for(String s: strs) {
System.out.print(s+ " ");
}
System.out.println();
}
in java 8, you can make use of stream
and lambda
:
ArrayList<String> result = su.solveNQueens(n)
result.stream().forEach(i->System.out.println(Arrays.toString(i)));
While you do System.out.println(su.solveNQueens(n));
it prints list contents which are array and it just prints array object, but not it's contents. so, to print array
contents you need to iterate through them.
Arrays.toString Returns a string representation of the contents of the specified array.
https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object)
get result into a list
and then print it:
List<String> res = su.solveNQueens(n);
for(String strs : res) {
System.out.println(Arrays.toString(strs));
}
You can do like this too as the above prints with ,
. Here iterating through the list and then array.
List<String> res = su.solveNQueens(n);
for(String strs : res) {
for(String s: strs) {
System.out.print(s+ " ");
}
System.out.println();
}
in java 8, you can make use of stream
and lambda
:
ArrayList<String> result = su.solveNQueens(n)
result.stream().forEach(i->System.out.println(Arrays.toString(i)));
edited Nov 20 at 4:09
answered Nov 20 at 3:58
secret super star
89813
89813
It works. Thank you. Can you please explain more? I am new to java. Thanks!
– Xinya Wang
Nov 20 at 4:01
Yes. Thank you very much!
– Xinya Wang
Nov 20 at 4:05
You explanation helps me a lot! Thank you very very much! ^_^
– Xinya Wang
Nov 20 at 4:17
add a comment |
It works. Thank you. Can you please explain more? I am new to java. Thanks!
– Xinya Wang
Nov 20 at 4:01
Yes. Thank you very much!
– Xinya Wang
Nov 20 at 4:05
You explanation helps me a lot! Thank you very very much! ^_^
– Xinya Wang
Nov 20 at 4:17
It works. Thank you. Can you please explain more? I am new to java. Thanks!
– Xinya Wang
Nov 20 at 4:01
It works. Thank you. Can you please explain more? I am new to java. Thanks!
– Xinya Wang
Nov 20 at 4:01
Yes. Thank you very much!
– Xinya Wang
Nov 20 at 4:05
Yes. Thank you very much!
– Xinya Wang
Nov 20 at 4:05
You explanation helps me a lot! Thank you very very much! ^_^
– Xinya Wang
Nov 20 at 4:17
You explanation helps me a lot! Thank you very very much! ^_^
– Xinya Wang
Nov 20 at 4:17
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53385985%2fhow-to-print-out-results-in-nqueens%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