int x=1;
int y=2;
x ^= y ^= x ^= y;
Run Code Online (Sandbox Code Playgroud)
我期待值被交换.但它给出x = 0和y = 1.当我尝试使用C语言时,它会给出正确的结果.
我遇到了一点混乱.
我知道那个String
对象是不可变的.这意味着如果我从String
类中调用一个方法,replace()
那么原始内容String
就不会改变.而是基于原始返回new String
.但是,可以为同一变量分配新值.
基于这个理论,我总是写a = a.trim()
在那里a
是一个String
.一切都很好,直到我的老师告诉我,a.trim()
也可以使用.这搞砸了我的理论.
我和老师一起测试了我的理论.我使用了以下代码:
String a = " example ";
System.out.println(a);
a.trim(); //my teacher's code.
System.out.println(a);
a = " example ";
a = a.trim(); //my code.
System.out.println(a);
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
example
example
example
Run Code Online (Sandbox Code Playgroud)
当我向老师指出时,她说,
这是因为我使用的是较新版本的Java(jdk1.7)并且
a.trim()
在以前版本的Java中工作.
请告诉我谁有正确的理论,因为我完全不知道!
我的老师告诉我,这是Bubble Sort的唯一代码
int a[]={2,3,7,9,8,1,4,5,10,6};
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length-i-1;j++)
{
if(a[j]>a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+"\t");
}
Run Code Online (Sandbox Code Playgroud)
但我用不同的外循环运行程序 -
int b[]={2,3,7,9,8,1,4,5,10,6};
for(int i=0;i<b.length-1;i++)
{
for(int j=0;j<b.length-i-1;j++)
{
if(b[j]>b[j+1])
{
int t=b[j];
b[j]=b[j+1];
b[j+1]=t;
}
}
}
for(int i=0;i<b.length;i++)
{
System.out.print(b[i]+"\t");
}
Run Code Online (Sandbox Code Playgroud)
输出是 - 第一案例 -
1 2 3 4 5 6 7 8 9 10
Run Code Online (Sandbox Code Playgroud)
第二个案例 -
1 2 3 4 5 6 7 8 9 10
Run Code Online (Sandbox Code Playgroud)
所以现在我被告知我的代码是错误的,即使我的输出是正确的.拜托,告诉我,我完全错了?
一开始,我承认我是个白痴;但有一件事我似乎无法理解。我似乎无法找到太大的区别ArrayList
和Vector
其他比这个:
“…… (这个类大致相当于 Vector,只不过它是不同步的。) ……”
在ArrayList的 javaDocs 中。
那么,当我们考虑一般场景(即排除基于线程的编程中的应用程序)时,这种差异真的是我们应用这两个相似类的方式的重要因素吗?还有其他明显的区别吗?如果是这样,请告诉我。如果不是,那么哪一种是最受青睐或普遍接受的方法?
好吧,我正在尝试做一些有条件的检查,并注意到这返回了假......我错过了什么?
int test = 1;
int[] testing= {1,3};
System.out.println(Arrays.asList(testing).contains(test) ); //false???
Run Code Online (Sandbox Code Playgroud) 这个问题是我在CodeReview上发布的回复的结果.
我有一个class
被调用的Point
,它基本上是"用于封装在2D空间中表示的点 ".我已经超越了以下hashcode()
功能:
...
@Override
public int hashCode() {
int hash = 0;
hash += (int) (Double.doubleToLongBits(this.getX())
^ (Double.doubleToLongBits(this.getX()) >>> 32));
hash += (int) (Double.doubleToLongBits(this.getY())
^ (Double.doubleToLongBits(this.getY()) >>> 32));
return hash;
}
...
Run Code Online (Sandbox Code Playgroud)
让我澄清(对于没有检查上述链接的人)我Point
使用两个double
s:x
并y
表示其坐标.
问题:
运行此方法时,我的问题很明显:
public static void main(String[] args) {
Point p1 = Point.getCartesianPoint(12, 0);
Point p2 = Point.getCartesianPoint(0, 12);
System.out.println(p1.hashCode());
System.out.println(p2.hashCode());
}
Run Code Online (Sandbox Code Playgroud)
我得到输出:
1076363264
1076363264
Run Code Online (Sandbox Code Playgroud)
这显然是个问题.基本上我打算 …
假设我有一个ArrayList < String >
namedarr
和一个StringBuilder
named buf
,我使用以下方法将内容添加到buf
to arr
。
if ( !buf.toString( ).equals( "" ) ) { //allow only non-empty Strings.
arr.add( buf.toString( ) );
}
Run Code Online (Sandbox Code Playgroud)
但它调用了buf.toString( )
两次。我不想在我的代码中出现这种情况。此外,还有其他(简短或简单或有效或其他)方法来实现这一目标吗?
编辑:另外,我在循环中执行此操作,那么这有效吗?
public Box<T>{
private T t;
public void setBox(T t){
this.t = t;
}
public List<T> toList(){
List<T> list = new ArrayList<>();
list.add(t);
return list;
}
}
Run Code Online (Sandbox Code Playgroud)
在这个简单的代码中这个toList()
方法通用与否?
谢谢...
我有一个interface
操作如下:
interface Operation {
public double calc(double a, double b);
}
Run Code Online (Sandbox Code Playgroud)
我用它来做enum
.
public enum Operations {
POWER("^", new Operation(){
public double calc(double a, double b) {
return Math.pow(a, b);
}
}),
MULTIPLICATION("*", new Operation(){
public double calc(double a, double b) {
return a * b;
}
}),
//...
private String op;
private Operation calc;
public Operations(String op, Operation calc) {
this.op = op;
this.calc = calc;
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用lambda表达式缩短我的代码,但我似乎无法正确使用语法.oracle教程对我来说有点太复杂了.有人可以用简单的方式向我解释一下吗?
编辑:我试过:
POWER ("^", (double a, …
我正在尝试使用用户输入所需的"0"来创建数字0.00000 .... 我的代码有什么问题?
int n;
double dec = 0.0;
in = new Scanner(System.in);
n = in.nextInt();
for (i = 1; i <= n; i++)
dec = dec / 10.0d;
Run Code Online (Sandbox Code Playgroud)
数字不会改变.
摘录自java.awt.Color
:
...
/**
* The color white. In the default sRGB space.
*/
public final static Color white = new Color(255, 255, 255);
/**
* The color white. In the default sRGB space.
* @since 1.4
*/
public final static Color WHITE = white; // My comment: THE SAME!!??
...
Run Code Online (Sandbox Code Playgroud)
从上面的摘录中可以看出,它Color
white
被分配给两个变量Color#WHITE
,Color#white
这也是同样的情况:
- black (and BLACK)
- blue (and BLUE)
- cyan (and CYAN)
- darkGray (and DARK_GRAY)
- gray …
Run Code Online (Sandbox Code Playgroud)