我正在尝试返回用户选择的文件.这很好.我可以在openFile中检查fileToOpen并且它是100%正确的,但是当我在main方法中sysout它时,我只是得到null.我想要用户选择的路径.
这是主要类:
public class Main {
public static void main(String[] args) {
File fileToOpen = null;
ReadIn openLog = new ReadIn();
openLog.openFile(fileToOpen);
System.out.println(fileToOpen);
}
}
Run Code Online (Sandbox Code Playgroud)
这是ReadIn类:
public class ReadIn extends JFrame{
public File openFile(File fileToOpen){
final JFileChooser fileChooser = new JFileChooser();
int modalToComponent=fileChooser.showOpenDialog(this);
if (modalToComponent == JFileChooser.APPROVE_OPTION) {
fileToOpen = fileChooser.getSelectedFile();
}
return fileToOpen;
}
}
Run Code Online (Sandbox Code Playgroud) 我试图在同一行上打印字符串和 int。但我得到一个错误。我知道解决这个错误的方法,但是为什么该行System.out.println("This is a string: %i", c2.a);给出错误而该行System.out.println("This is class method" + c2.a );给出正确的输出。下面是我的代码。
public class MyClass
{
private int a;
public double b;
public MyClass(int first, double second)
{
this.a = first;
this.b = second;
}
// new method
public static void incrementBoth(MyClass c1) {
c1.a = c1.a + 1;
c1.b = c1.b + 1.0;
}
//pass by valuye therefore no change
public static void incrementA(int a)
{
a = a+1;
}
public static void main(String[] …Run Code Online (Sandbox Code Playgroud) 我很困惑为什么我不能使用该语句来显示数组中的值?
System.out.println(intarray);
这是输出“[I@7852e922”
如果 java 是按值传递的,那么该语句不应该返回数组的值而不是对象引用。这是我使用的修复方法。
System.out.println(Arrays.toString(intarray));
Run Code Online (Sandbox Code Playgroud) https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
此代码用于生成所有可能的字符串,当2到9(含)之间的每个数字都映射到英文字母字符串时.
问题是重新创建移动电话T9字典,这意味着,如果用户键入23,则应返回"abc"和"def"字符串的所有可能组合.
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(letterCombinations("23"));
}
private static final String[] KEYS = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
public static List<String> letterCombinations(String digits) {
List<String> ret = new LinkedList<String>(); //this is the local variable.
combination("", digits, 0, ret); //being sent …Run Code Online (Sandbox Code Playgroud) 我有两个列表(listA和listB),我希望它们是独立的。因此,我可以将一个元素添加到listB,而无需修改listA的元素,以保持不变。
已尝试收集listB = Collections.unmodifiableCollection(listA);
ArrayList<String> listA = new ArrayList<>();
listA.add("alex");
listA.add("brian");
listA.add("charles");
ArrayList<String> listB = new ArrayList<>();
listB = listA;
listB.add("williams");
System.out.println(listA);
Run Code Online (Sandbox Code Playgroud)
执行:[alex,brian,查尔斯,威廉斯]
当我运行它时,我只想显示这些
运行:[alex,brian,charles]
(不带“ williams”)
我需要从 中选取 N 个随机元素List<MyObject> objects。MyObject是一个复杂的类对象。我可以很容易地Collections.shuffle(objects)得到一个大小为 N 的子列表。
但后来我想到了一个想法。如果我创建 a List<Integer> indexes= [0, N-1],改洗此索引数组,获取一个子列表,并使用它从原始列表中获取元素,它会提高性能吗?
我的问题归结为:在 Java 中重排与轻量级对象列表之间是否存在有意义的性能差异?
ps:如果重要的话,我需要在新列表中返回随机选择,同时还将它们从原始列表中删除。
可能重复:
Java是"传递引用"吗?
为什么下面的代码打印'test'而不是抛出NullPointerException?
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("test");
append(sb);
System.out.println(sb.toString());
}
public static void append(StringBuilder sb) {
sb = null;
}
Run Code Online (Sandbox Code Playgroud) 我正在创建一个HashMap,如下所示:
private HashMap <String, JSONArray> HiveMap;
HiveMap = new HashMap <String, JSONArray>();
Run Code Online (Sandbox Code Playgroud)
还有一个私有类变量来存储一些计算数据,这些数据将在计算完成后放入HashMap:
private JSONArray hiveList;
Run Code Online (Sandbox Code Playgroud)
在计算hiveList之后,我将hiveList放入创建的HashMap(HiveMap)中:
HiveMap.put(hiveNode,hiveList);
Run Code Online (Sandbox Code Playgroud)
当我现在打印出HiveMap时,我得到的输出是预期的,包含字段hiveNode和hiveListHashmap中的现有字段.<"hiveNode":"hiveList">
完成后,我假设我放入HashMap的数据hiveList将继续存在,所以我清除了hiveList数组hiveList.clear();
但是当我在清除hiveList之后打印Hashmap时,我发现来自hiveList的数据也从HashMap中消失了.
清除hiveList后打印HashMap会导致: <"hiveNode": >
我不明白这种行为,如果有人能对此有所了解,我将不胜感激.
您好我正在研究gcd和逆模运算的算法.
我必须使用BigInteger类,但我有一些问题.
那你能帮我吗?
问题是java programm不想用新的输入覆盖旧的BigInteger输入.
import java.math.BigInteger;
public class Gcd
{
public static void gcd(BigInteger a, BigInteger b){
BigInteger modulo = b;
BigInteger wert = a;
BigInteger zwischenwert1 = BigInteger.valueOf(1);
BigInteger zwischenwert2 = BigInteger.valueOf(0);
BigInteger zwischenwert3 = BigInteger.valueOf(0);
BigInteger zwischenwert4 = BigInteger.valueOf(1);
BigInteger negativ = BigInteger.valueOf(-1);
BigInteger q;
do{
q = modulo.divide(wert);
wert = modulo.subtract(wert.multiply(q));
zwischenwert3 = zwischenwert1.subtract(zwischenwert3.multiply(q));
zwischenwert4 = zwischenwert2.subtract(zwischenwert4.multiply(q));
modulo = negativ.multiply((wert.subtract(modulo)).divide(q));
zwischenwert1 = negativ.multiply((zwischenwert3.subtract(zwischenwert1)).divide(q));
zwischenwert2 = negativ.multiply((zwischenwert4.subtract(zwischenwert2)).divide(q));
}while((modulo.signum()>1)&&(wert.signum()>1));
System.out.println("gcd("+a+","+b+") = "+zwischenwert3+" * "+b+ " + "+ zwischenwert4+" …Run Code Online (Sandbox Code Playgroud) 我是 Java 的初学者,最近遇到了一个关于 Java String 概念的面试问题:
public class Test1 {
public static void changeStr(String str){
str="welcome";
}
public static void main(String[] args) {
String str="1234";
changeStr(str);
System.out.println(str);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为输出应该是“welcome”,但是,我在 Eclipse 中对其进行了测试,它显示“1234”,Java 字符串不是引用,因此在方法 changeStr 中将 Java 字符串“str”引用更改为“welcome” ?
请原谅我的初学者问题!