java列表包含列表_java – 包含对列表

你的Pair类需要实现equals()和hashCode(),你们都已经设置好了. List.contains()是根据类型的equals()方法实现的.请参阅

API for List.contains().(编辑了一下,以回应@maaartinus的评论,你的回答你应该读到b / c,观察是可靠的,我把它们折叠在这里有点荒谬.正如maaartinus指出的那样,最好的做法这里将避免容易出错的手动定义equals和hashcode,而是建立在Guava的0700和

hashCode for n objects的辅助函数上.

final class Pair {

final T left;

final T right;

public Pair(T left, T right)

{

if (left == null || right == null) {

throw new IllegalArgumentException("left and right must be non-null!");

}

this.left = left;

this.right = right;

}

public boolean equals(Object o)

{

// see @maaartinus answer

if (! (o instanceof Pair)) { return false; }

Pair p = (Pair)o;

return left.equals(p.left) && right.equals(p.right);

}

public int hashCode()

{

return 7 * left.hashCode() + 13 * right.hashCode();

}

}

使用合适的equals(),您现在可以:

lp.add(new Pair("1", "2"));

assert lp.contains(new Pair("1","2"));

回应下面的评论,或许最好包含一个很好的参考“我为什么需要实现hashCode()?”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jBullet是一个Java实现的开源物理引擎,可以用于模拟2D和3D物理场景。以下是一个简单的jBullet示例,演示如何创建一个简单的物理场景并模拟物体的运动。 首先,我们需要导入jBullet库: ```java import com.bulletphysics.collision.shapes.*; import com.bulletphysics.dynamics.*; import com.bulletphysics.linearmath.*; import javax.vecmath.Vector3f; ``` 然后,我们可以创建一个物理场景: ```java DynamicsWorld world = new DiscreteDynamicsWorld(new CollisionDispatcher(), new DbvtBroadphase(), new SequentialImpulseConstraintSolver(), new DefaultCollisionConfiguration()); ``` 这里我们使用了一个离散的动力学世界,它包含了一个碰撞检测器、一个宽相检测器、一个顺序脉冲约束求解器和一个默认的碰撞配置。 接下来,我们可以创建一个平面作为地面: ```java StaticPlaneShape groundShape = new StaticPlaneShape(new Vector3f(0, 1, 0), 0); DefaultMotionState groundMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(0, -1, 0), 1.0f)))); RigidBodyConstructionInfo groundRigidBodyCI = new RigidBodyConstructionInfo(0, groundMotionState, groundShape, new Vector3f(0, 0, 0)); RigidBody groundRigidBody = new RigidBody(groundRigidBodyCI); world.addRigidBody(groundRigidBody); ``` 这里我们创建了一个静态平面,沿着y轴为1,位于y轴为0处。我们还创建了一个默认的运动状态,并使用它来创建一个刚体。最后,我们将刚体添加到物理世界中。 现在,我们可以创建一个球体,并给它一个随机的初速度: ```java float ballRadius = 1.0f; SphereShape ballShape = new SphereShape(ballRadius); Vector3f ballInertia = new Vector3f(0, 0, 0); ballShape.calculateLocalInertia(1, ballInertia); DefaultMotionState ballMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f((float) (Math.random() * 10), 20, (float) (Math.random() * 10)), 1.0f)))); RigidBodyConstructionInfo ballRigidBodyCI = new RigidBodyConstructionInfo(1, ballMotionState, ballShape, ballInertia); RigidBody ballRigidBody = new RigidBody(ballRigidBodyCI); ballRigidBody.setLinearVelocity(new Vector3f((float) (Math.random() * 10), -10, (float) (Math.random() * 10))); world.addRigidBody(ballRigidBody); ``` 这里我们创建了一个球体,并使用calculateLocalInertia方法计算了它的惯性。我们还创建了一个随机的运动状态,并使用它来创建一个刚体。我们将刚体添加到物理世界中,并为球体设置一个随机的初速度,以便它开始运动。 最后,我们可以模拟物理场景并更新球体的位置: ```java for (int i = 0; i < 100; i++) { world.stepSimulation(1 / 60f, 10); Vector3f ballPos = new Vector3f(); ballRigidBody.getMotionState().getWorldTransform(new Transform()).getTranslation(ballPos); System.out.println("Ball position: " + ballPos.x + ", " + ballPos.y + ", " + ballPos.z); } ``` 这里我们循环100次,每次调用stepSimulation方法模拟物理场景,并使用getWorldTransform方法获取球体的位置并输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值