java对象引用改变地址,Java:更改对象值而不更改所有引用

This is probably a noob question, but i just don't get it.

I am trying to implement A* pathfinding in my game. I was following this tutorial and the code in the AstarPathfinder.java. But instead of instantiating the AStarPathfinder class and having an 2D Array for all Nodes i made a static method, to which i pass my 2D Array (the Level/World), start and end Node.

Cause of this i always have to store the current and the next Node in a Node next and Node current. I then add them to the open or closed list (depends on where they belong) and change the x and y value (position) of the Node with their setter method, to have the next node.

After a few minutes of debuging i noticed that this (ofc) changes also the value of the node inside the openList and closedList. To prevent this i can simply call next = new Node(int x, int y) instead of only setting the values, but as my Pathfinding is running every few render loops it would mess up garbage collection and cost a lot of performance. So i am looking for a possibility to have one Node variable, which i can instantiate once with default constructor, and then change its value without changing its value in the list.

Is that possible somehow?

解决方案

While this is not a direct answer to your question, hopefully it will help you make a good decision

The JVM has become very good at re-using objects and you should generally not be afraid to use the new keyword.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值