使用clone()vs new obj(using clone() vs new obj)
我正在寻找Cracking the Code Interview和这个问题。 我应该在二叉树中找到具有给定数字之和的所有路径。 我通常理解这段代码,但我想知道为什么答案使用
clone()
而不是用new
创建一个新对象?public static void findSum(BinaryTreeNode root, int sum, ArrayList<Integer> buffer, int level) { // done parsing or empty tree if (root == null) { return; } int tmp = sum; buffer.add(root.value); for (int i = level; i >= 1; i--) { tmp -= buffer.get(i); if (tmp == 0) { print(buffer, i, level); } } ArrayList<Integer> c1 = (ArrayList<Integer>) buffer.clone(); ArrayList<Integer> c2 = (ArrayList<Integer>) buffer.clone(); findSum(root.left, sum, c1, level + 1); findSum(root.left, sum, c2, level + 1); } public static void print(ArrayList<Integer> bugger, int level, int i2) { for (int i = level; i <= i2; i++) { System.out.print(buffer.get(i) + " "); } System.out.println(); }
I am looking over Cracking the Code Interview and in this problem. I am supposed to find all paths in a binary tree that has the sum of a given number. I generally understand this code but I am wondering why the answer uses
clone()
instead of creating a new object withnew
?public static void findSum(BinaryTreeNode root, int sum, ArrayList<Integer> buffer, int level) { // done parsing or empty tree if (root == null) { return; } int tmp = sum; buffer.add(root.value); for (int i = level; i >= 1; i--) { tmp -= buffer.get(i); if (tmp == 0) { print(buffer, i, level); } } ArrayList<Integer> c1 = (ArrayList<Integer>) buffer.clone(); ArrayList<Integer> c2 = (ArrayList<Integer>) buffer.clone(); findSum(root.left, sum, c1, level + 1); findSum(root.left, sum, c2, level + 1); } public static void print(ArrayList<Integer> bugger, int level, int i2) { for (int i = level; i <= i2; i++) { System.out.print(buffer.get(i) + " "); } System.out.println(); }
原文:https://stackoverflow.com/questions/21466265
最满意答案
你想在屏幕中居中一个JFrame吗? 如果是这样,那么在调用
pack()
之后,只需在JFrame上调用setLocationRelativeTo(null)
pack()
。 如果你可以避免它,你真的不想设置JFrame的大小,而是让你的布局管理器为你做大小设置。pack()
方法将要求容器的布局管理器布置其组件并为其组件设置最佳大小。编辑
关于你的问题:谢谢,这很棒。 您有机会告诉我在docs.oracle中可以找到方法详细信息的位置
由于可以在JFrame上调用此方法,只需转到JFrame API即可 。 如果这是父类的方法,API将告诉您并提供父类及其方法的链接。
Do you want to center a JFrame in the screen? If so then simply call
setLocationRelativeTo(null)
on the JFrame after callingpack()
. You really don't want to set the size of the JFrame if you can avoid it, but instead have your layout managers do the size setting for you. Thepack()
method will ask the containers' layout managers to lay out their components and set the best sizes for their components.Edit
Regarding your question:Thanks, that's great. Any chance you could tell me where I could find the method details within docs.oracle
Since this method can be called on a JFrame, simply go to the JFrame API. If this is a method of a parent class, the API will tell you and provide the link to the parent class and its method.
相关问答
更多-
如果你有一条从(ax,ay)到(bx,by)的线,那么该线的中点只是((ax + bx)/ 2.0,(ay + by)/ 2.0))。 If you have a line that goes from (ax,ay) to (bx, by), the mid-point of the line is just ((ax + bx) / 2.0, (ay + by) / 2.0)).
-
中点圈没有重复?(Midpoint circle without duplicates?)[2023-09-10]
如果考虑代码正在做什么,有两种情况会产生重复:当y为0(沿着图的边缘),以及当x == y (圆圈中的对角线)时。 您可以在对这些条件进行适当的coord计算之前添加检查以排除它们。 例如, coord = { start.x + x, start.y + y }; 和coord = { start.x + x, start.y - y }; 当y为零时生成相同的值。 If you consider what your code is doing, there are two cases that gene ... -
如何实现中点位移(How to implement midpoint displacement)[2021-08-22]
答案非常简单,顺便说一句,我给你留下了深刻的印象,你设法调试代码中所有潜在的错误。 以下行错了: displacedLine[center - 1] += change; 您正确计算了中心索引并更改了数量,但您错过了更改应该应用于高度方面的中点 。 那是: displacedLine[center - 1] = (displacedLine[start] + displacedLine[end]) / 2; displacedLine[center - 1] += change; 我相信你明白了。 Th ... -
谷歌地图中的路线中点(Midpoint of route in google maps)[2022-04-29]
计算路线中点的示例 使用Mike Williams的epoly库的v3版本。 var directionDisplay; var directionsService = new google.maps.DirectionsService(); var map; var polyline = null; var infowindow = new google.maps.InfoWindow(); function createMarker(latlng, label, html) { ... -
SQLite圆中点(SQLite round midpoint)[2022-04-09]
所以我一直在尝试使用Colonel Thirty Two提示,使用Binding函数 ,但sqlite-net不提供这样的功能。 我转向System.Data.SQLite.dll,以帮助其他人: 首先在c#中创建自定义函数 [SQLiteFunction(Name = "RoundAccounting", Arguments = 2, FuncType = FunctionType.Scalar)] public class RoundAccounting : SQLiteFunction { p ... -
计算捕捉的中点(Calculate midpoint for snapping)[2023-10-09]
我找到了解决方案: var middlePoints=[]; function createPoint(p1,p2){ var xC,yC; xC=(p1.x+p2.x)/2; yC=(p1.y+p2.y)/2; var point= new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(xC,yC)); point.style={strokeColor:"red",strokeOpacity:0.3, ... -
你想在屏幕中居中一个JFrame吗? 如果是这样,那么在调用pack()之后,只需在JFrame上调用setLocationRelativeTo(null) pack() 。 如果你可以避免它,你真的不想设置JFrame的大小,而是让你的布局管理器为你做大小设置。 pack()方法将要求容器的布局管理器布置其组件并为其组件设置最佳大小。 编辑 关于你的问题: 谢谢,这很棒。 您有机会告诉我在docs.oracle中可以找到方法详细信息的位置 由于可以在JFrame上调用此方法,只需转到JFrame API即 ...
-
正如Mark Dickinson在对该问题的评论中指出的那样,ECMA-262 ECMAScript语言规范要求使用IEEE 754 64位二进制浮点来表示数字类型 。 相关的舍入规则是“选择该组中与x最接近的成员。如果该组的两个值相等,则选择具有偶数有效数的值......”。 这些规则是通用的,适用于算术的舍入结果以及文字的值。 以下是在IEEE 754 64位二进制浮点中可准确表示的问题的相关范围内的所有数字。 每个都显示为十进制值,也作为其位模式的十六进制表示。 具有偶数有效数的数字在其位模式中具有最 ...
-
假设A , B , M和Center是某些矢量类型的对象,通常的操作如下: var a = A-C; var b = B-C; var m = a+b; m是从Center向M的向量。 因此: m = m.Normalize() * Radius; M = Center + m; 请注意:此算法不假设A和B的顺序,并且始终将弧解释为两个可能的中的较小者。 在不添加特殊情况的情况下,它只能处理角度小于180°的圆弧。 处理顺序:首先使用atan2计算从a到b的角度: var angle = Math.At ...
-
else if(respons == higher) { min = min + 1; getMidpoint(min,max); } else if(respons == lower) { max = min - 1; getMidpoint(min,max); } 这部分很奇怪。 如果猜测应该更高 ,你应该做min = midpoint + 1而不是仅将min增加1.当它应该更低时 ,你应该做max = midpoint - 1 。 你 ...