我有这么简单的代码:
import java.util.ArrayList;
import java.util.List;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Integer[] tab = {2324,1,2,2324,3,45,1,5,0,9,13,2324,1,3,9,8,4,2,1};
Integer max = 2324;
List<Integer> indexes = new ArrayList<Integer>();
for (int e = 0; e < tab.length; e++) {
if (tab[e] == max) {
indexes.add(new Integer(e));
System.out.println("Found max");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里的主要问题是我想在我tab的max值中找到每个索引.现在,它不起作用 - 它甚至没有显示发现最大消息一次,虽然它应该做3次.那问题呢?
好的,这终于奏效了,谢谢你们所有人:
public static void main(String[] args) {
Integer[] tab = {2324,1,2,2324,3,45,1,5,0,9,13,2324,1,3,9,8,4,2,1};
Integer max …Run Code Online (Sandbox Code Playgroud) 在 .NET 中,在比较 Type 对象时我可以不用 == 。是否可以使用相同的运算符来比较 Java 中的 Class 对象,还是应该始终使用 equals() 方法?
import java.util.Scanner;
public class Poop {
public static void main (String args[]){
Scanner input = new Scanner(System.in);
String Gender;
System.out.println("Are you a boy or a girl?");
Gender = input.nextLine();
if(Gender == "boy"){
System.out.println("You are a boy.");
}
if(Gender == "girl"){
System.out.println("You are a girl.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么这个程序不起作用.在Eclipse中,它表示没有错误但是当我运行它并输入男孩或女孩时没有任何反应,我不明白为什么.
另外请不要取笑我在字符串变量上测试自己的程序.
可以说有一个包含一系列对象的数组,
Object[] list;
Run Code Online (Sandbox Code Playgroud)
例如,设计用于遍历此数组的方法
public boolean contains(Object e)
{
for(Object e_:list)
if(e_.equals(e))
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
我很困惑的是for循环如何迭代数组.在迭代时,它是否将e_分配给与list [index]相同的内存位置,或者是e_从list [index]克隆的新对象,因为我想要做的是使用==而不是equals()以便我可以具体使用调用该对象,而不是冒险将它与另一个对等.我理解我可以覆盖equals()并使其成为最终以防止继承成为一个问题,但我想了解迭代是如何工作的.你最好用外行的方式回答,而不仅仅是把我送到另一个网站,因为我一直在oracle和其他论坛上,我读到的解释有点过头了.
下列:
class ArrayCompare
{
public static void main (String[] args)
{
int []arr1 = {1, 2, 3, 4, 5};
int []arr2 = {1, 2, 3, 4, 5};
System.out.println("arr1 == arr2 is " + (arr1 == arr2));
}
}
Run Code Online (Sandbox Code Playgroud)
返回 arr1 == arr2 is false
为什么是这样?为什么arr1 Equals to arr2是false.
这不是真的吗?
我写了一个测试代码来检查相等性.我检查过Java doc,它说BigInteger是不可变的.检查静态工厂方法的文档,valueOf它看起来像返回已经缓存的不可变实例.那么为什么==在缓存的实例时返回false.
以下是适用valueOf于的Java文档BigInteger:
返回一个Big Integer,其值等于指定long的值.这种"静态工厂方法"优先于(长)构造函数提供,因为它允许重用常用的BigIntegers.
下面的代码将进入无限循环.
public static void main(String[] args) {
while(true) {
BigInteger a = BigInteger.valueOf(100);
BigInteger c = BigInteger.valueOf(100);
if (a == c) {
break;
}
}
Run Code Online (Sandbox Code Playgroud) 比方说我有C#LINQ查询如下:
var allBooks = from book in books
select book;
var booksNonFiction = allBooks.Where(x => x.Genre = NonFiction)
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我想获得集合中的所有NonFiction标题allBooks.我对lambda表达有点不确定.
我有一个对象列表,这些对象实际上是棋子。
每个对象都包含价格名称及其在国际象棋桌上的位置。名字K用于国王、Q王后、R车……等等。
所以我有一个ArrayList<Enemy> chesspieces. 列表没有排序,元素可能是这样的:
P,P,P,P,P,P,P,P,R,N,B,Q,K,B,N,R.
Run Code Online (Sandbox Code Playgroud)
我想创建某种prioritySort,有一个这样的列表:
K,Q, R, R, B, B, N,N,R,R P,P,P,P,P,P,P,P
Run Code Online (Sandbox Code Playgroud)
我开始做一些事情,但我看到它是如何有缺陷的,我不确定如何实现,这是我到目前为止所做的
这是我更新的 Enemy 课程
public class Enemy implements Comparable {
public Piece name;
public int rank;
public int file;
public String position;
private int value;
public Enemy(Piece name, int file, int rank, String position) {
this.name = name;
this.rank = rank;
this.file = file;
this.position = position;
}
public int getValue(Piece name) {
if (name.toString() == "k") value …Run Code Online (Sandbox Code Playgroud) 我想比较两个对象。但是,它告诉我当我运行它时它们不匹配,即使它们匹配。请让我知道我做错了什么,谢谢。这是我的代码:
Player p1 = new Player("Mo", "Food", 200.0,0.0);
Player p2 = new Player("Mo", "Food", 200.0,0.0);
System.out.println(p1.equals(p2)); // -- false
Run Code Online (Sandbox Code Playgroud) 我正在学习Overriding hashCode()和课堂equals(Object obj)方法Object.
类中的equals(Object obj)方法体Object是:
public boolean equals(Object obj) {
return (this == obj);
}
Run Code Online (Sandbox Code Playgroud)
并hashCode()是native:
public native int hashCode();
Run Code Online (Sandbox Code Playgroud)
我有一个类Test有overrided equals(Object obj)和hashCoe():
public class Test {
public static void main(String[] args){
Test t1 = new Test();
Test t2 = new Test();
System.out.println("t1 toString() : " + t1.toString());
System.out.println("t1, Hex value of hashcode : " + Integer.toHexString(t1.hashCode()));
System.out.println("t2 toString() : " + …Run Code Online (Sandbox Code Playgroud)