我做了这个递归方法,计算二叉树中最长的路径.它的存储路径在一个集会上然后返回.但是,我不得不声明数组列表变量全局.是否有可能使这个方法,但他的数组列表变量是本地的.
public static <T> ArrayList<T> longestPath(BinaryNode<T> root){
//ArrayList path = new ArrayList();
if(root == null) return null;
if(height(root.left) > height(root.right)){
path.add(root.element);
longestPath(root.left);
}else{
path.add(root.element);
longestPath(root.right);
}
return path;
}
Run Code Online (Sandbox Code Playgroud)
我必须使它成为全局的原因是因为它是一个递归程序,每次它调用自身它将创建一个具有差异地址的新数组列表对象变量,如果你知道我的意思.
我正在尝试用Java解析Kml文件.因为我需要获取地标的坐标,在java中生成一个poligon并使用它.
但我的问题是,我正在使用JAK这个库解析它,我无法提取我想要的信息.(我在官方页面中读到了"帮助",但我没有找到任何帮助解决我的问题)
我正在尝试这样做:
final Kml kml = Kml.unmarshal(new File("C:/Users/A556520/Documents/Proyectos/GeoFencing/res/labasa.kml"));
final Document document = (Document)kml.getFeature();
List<Feature> listafeatures = document.getFeature();
Run Code Online (Sandbox Code Playgroud)
但在这一点上我不知道如何提取坐标.
我试图解析的文件就是这个:la basa
我有一个性能问题,当使用try catch子句时,最好指定你可以得到的确切异常,或者只是使用异常它更好?例:
try {
whatever
} catch (NullPointerException ex) {
whatever
}
Run Code Online (Sandbox Code Playgroud)
或者如果你不介意什么样的例外:
try {
whatever
} catch (Exception ex) {
whatever
}
Run Code Online (Sandbox Code Playgroud)
因为我知道你可以使用不同的异常来触发不同的效果,但我只是要求性能.
我正在尝试为JAVA中的银行应用程序制作GUI.如果我在eclipse中使用WindowBuilder,那么使用我的框架的绝对布局很容易创建任何东西,但问题是我调整框架的大小.这就是为什么我选择使用gridBagLayout,我可以使用weightx/y来让我的工作更轻松.我有点忘了如何正确使用这个布局,所以我第一次尝试在gridBagLayout主面板中添加JPanel时遇到困难.
这是我想要实现的(绝对布局):

这就是我所拥有的(在gridBagLayout中制作):

如果有人可以在我第一次添加flowLayout面板时指出我需要更改/添加的内容,我将不胜感激.基本上是第一个细胞的白色空间 - 我想摆脱它并控制它!
这是一些代码:
setBackground(new Color(255, 255, 255));
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JPanel panel = new JPanel();
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
flowLayout.setVgap(15);
flowLayout.setHgap(15);
flowLayout.setAlignment(FlowLayout.LEFT);
panel.setBackground(new Color(51, 102, 204));
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.anchor = GridBagConstraints.NORTH;
gbc_panel.fill = GridBagConstraints.HORIZONTAL;
gbc_panel.gridx = 0;
gbc_panel.gridy = 0;
add(panel, gbc_panel);
JLabel label = new JLabel("European Bank");
label.setForeground(Color.WHITE); …Run Code Online (Sandbox Code Playgroud) 我试图想出一个将经历一个弹簧的循环,一旦它到达%字符,它就会将%之后的所有内容传递给hexToInt函数.这就是我想出来的.
for(int x=0; x<temp.length(); x++)
{
if(temp.charAt(x)=='%')
{
newtemp = everthing after '%'
hexToInt(newtemp);
}
}
Run Code Online (Sandbox Code Playgroud) 当我用来openStream()从Web服务解析XML时,我收到错误:
无法打开流
我检查了URL,它仍然有效.
URL url = new URL("http://uitbookshop.php0h.com/PHPService/findbyname.php?name=thu");
Log.d("search", "getXML::url "+url.toString());
InputStream iS;
try {
iS = url.openStream();
doc=db.parse(new InputSource(new InputStreamReader(iS, Charset.forName("utf-8"))));
} catch (IOException e) {
Log.e("search", "Can not open stream");
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) Java(或任何其他第三方库)是否提供了一个API来替换基于字符代码的字符(Charset当然是在已知的内部)而不是正则表达式?例如,要用给定字符串中的单引号替换双引号,可以使用:
String noDoubles = containsDoubles.replace("\"", "'");
Run Code Online (Sandbox Code Playgroud)
但是双引号的UTF-8字符代码是U+0022.那么有什么东西可以搜索U+0022字符的实例并用单引号替换它们吗?
此外,不只是在这里询问双/单引号,我在谈论字符代码查找和替换任意2个字符.
尽管在设置文本之前设置了id,但事件处理程序之前的代码中的Text对象并没有改变颜色。但是,文本将随着文本设置的填充而改变颜色。我很好奇的一件事是,在CSS中,颜色不会随着.text{}OR 改变#Sign-In-Text{}。我只是不正确地使用CSS吗?
Java代码:
public class CSSTrial1 extends Application {
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("CSS Trial");
stage.setResizable(false);
final GridPane grid = new GridPane();
ColumnConstraints constraints = new ColumnConstraints();
grid.getColumnConstraints().add(constraints);
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25));
grid.add(new Label("Character Name:"), 0, 0);
final TextField nameField = new TextField("");
nameField.setPromptText("Randel James");
grid.add(nameField, 1, 0);
grid.add(new Label("Weapon:"), 0, 1);
final ComboBox<String> weapons = new ComboBox<>();
weapons.getItems().addAll("Katsuragi x2", "Assault Rifle",
"Pistol", "RPG-7", "Barret 50. Cal");
grid.add(weapons, 1, 1);
HBox …Run Code Online (Sandbox Code Playgroud) 如果我有一个具有Map命名myMap和getter的类:
public Map getMap() {
synchronized(myMap) {
return myMap;
}
}
Run Code Online (Sandbox Code Playgroud)
难道getMap().put(Something)也会被同步?
请告诉我,如果我正在应用Tell,请不要在此示例中以正确的方式询问原则。
我有两个类,CalculationResults有一个函数calculateMe(),Data用于进行一些计算。重要的是它存储结果。
class Data {
private:
int dataForCalculations;
public:
void calculate(CalculationResults& calculationResults) {
calculationResults.calculateMe(dataForCalculations);
}
};
class CalculationResults {
private:
int calculatedData;
public:
void calculateMe(int dataForCalculations) {
calculatedData = someCalculations(dataForCalculations);
}
};
// somewhere else:
Data data;
CalculationResults calculationResults;
data.calculate(calculationResults);
Run Code Online (Sandbox Code Playgroud)
这段代码的第一个版本(在应用Tell之前,不要问)没有使用Data::calculatefunction 但它有一个getterfor dataForCalculations,所以我在某处调用了calculationResults.calculateMe(data.getDataForCalculations()).
新版本更好吗?