首页 \ 问答 \ 使用clone()vs new obj(using clone() vs new obj)

使用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 with 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();
}

原文:https://stackoverflow.com/questions/21466265
更新时间:2024-01-19 09:01

最满意答案

你想在屏幕中居中一个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 calling pack(). 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. The pack() 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)).
  • 如果考虑代码正在做什么,有两种情况会产生重复:当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 ...
  • 答案非常简单,顺便说一句,我给你留下了深刻的印象,你设法调试代码中所有潜在的错误。 以下行错了: displacedLine[center - 1] += change; 您正确计算了中心索引并更改了数量,但您错过了更改应该应用于高度方面的中点 。 那是: displacedLine[center - 1] = (displacedLine[start] + displacedLine[end]) / 2; displacedLine[center - 1] += change; 我相信你明白了。 Th ...
  • 计算路线中点的示例 使用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) { ...
  • 所以我一直在尝试使用Colonel Thirty Two提示,使用Binding函数 ,但sqlite-net不提供这样的功能。 我转向System.Data.SQLite.dll,以帮助其他人: 首先在c#中创建自定义函数 [SQLiteFunction(Name = "RoundAccounting", Arguments = 2, FuncType = FunctionType.Scalar)] public class RoundAccounting : SQLiteFunction { p ...
  • 我找到了解决方案: 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 。 你 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。