Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 173b9fc

Browse files
committed
Merge pull request giantray#21 from tangculijier/master
Create What's-the-simplest-way-to-print-a-Java-array.md
2 parents df2d6ac + 2cc5875 commit 173b9fc

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
给3个布尔变量,当其中有2个或者2个以上为true菜返回true
2+
===
3+
问题
4+
给3个boolean变量,a,b,c,当其中有2个或2个以上为true时才返回true?
5+
最笨的方法:
6+
```java
7+
boolean atLeastTwo(boolean a, boolean b, boolean c)
8+
{
9+
if ((a && b) || (b && c) || (a && c))
10+
{
11+
return true;
12+
}
13+
else
14+
{
15+
return false;
16+
}
17+
}
18+
```
19+
* 优雅解法1
20+
```java
21+
return a ? (b || c) : (b && c);
22+
```
23+
24+
* 优雅解法2
25+
```java
26+
return (a==b) ? a : c;
27+
```
28+
29+
* 优雅解法3
30+
```java
31+
return a ^ b ? c : a
32+
```
33+
34+
* 优雅解法4
35+
```java
36+
return a ? (b || c) : (b && c);
37+
```
38+
39+
stackoverflow链接: http://stackoverflow.com/questions/3076078/check-if-at-least-two-out-of-three-booleans-are-true
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
java中打印一个数组最简单的方法是什么
2+
===
3+
问题
4+
---
5+
在java中,数组没有重写toString()方法,所以我如果直接调用数组toStrign()方法的话,我会知道得到它的内存地址。像这样:
6+
```java
7+
int[] intArray = new int[] {1, 2, 3, 4, 5};
8+
System.out.println(intArray); // 有时候会输出 '[I@3343c8b3'
9+
```
10+
但是实际上我想要的输出效果是
11+
```java
12+
[1, 2, 3, 4, 5]
13+
```
14+
所以打印一个数组最简单的方法是什么?我想要的效果是
15+
```java
16+
// 数字数组:
17+
int[] intArray = new int[] {1, 2, 3, 4, 5};
18+
//输出: [1, 2, 3, 4, 5]
19+
20+
// 对象数组:
21+
String[] strArray = new String[] {"John", "Mary", "Bob"};
22+
//输出: [John, Mary, Bob]
23+
```
24+
25+
回答
26+
---
27+
在JAVA5中使用 Arrays.toString(arr) 或 Arrays.deepToString(arr)来打印数组。
28+
29+
不要忘了引入import java.util.Arrays;
30+
```java
31+
package packageName;
32+
import java.util.Arrays;
33+
```
34+
35+
```java
36+
int[] intArray = new int[] {1, 2, 3, 4, 5};
37+
System.out.println(Arrays.toString(intArray));
38+
//输出: [1, 2, 3, 4, 5]
39+
40+
String[] strArray = new String[] {"John", "Mary", "Bob"};
41+
System.out.println(Arrays.deepToString(strArray));
42+
*//输出: [John, Mary, Bob]
43+
```
44+
Arrays.deepToString与Arrays.toString不同之处在于,Arrays.deepToString更适合打印多维数组<br>
45+
比如: <br>
46+
47+
```java
48+
String[][] b = new String[3][4];
49+
for (int i = 0; i < 3; i++)
50+
{
51+
for (int j = 0; j < 4; j++)
52+
{
53+
b[i][j] = "A" + j;
54+
}
55+
}
56+
System.out.println(Arrays.toString(b));
57+
//输出[[Ljava.lang.String;@55e6cb2a, [Ljava.lang.String;@23245e75, [Ljava.lang.String;@28b56559]
58+
System.out.println(Arrays.deepToString(b));
59+
//输出[[A0, A1, A2, A3], [A0, A1, A2, A3], [A0, A1, A2, A3]]
60+
61+
```
62+
stackoverflow链接: http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /