由于某种原因,我的方法perfectShuffle(int [] values)正在改变传递给它的参数,请告诉我为什么这是......这对我没有意义,因为Java是一种"通过价值传递"编程语言.
public class Shuffler {
/**
* The number of consecutive shuffle steps to be performed in each call to
* each sorting procedure.
*/
private static final int SHUFFLE_COUNT = 4;
/**
* The number of values to shuffle.
*/
private static final int VALUE_COUNT = 5;
/**
* Tests shuffling methods.
*
* @param args
* is not used.
*/
public static void main(String[] args) {
System.out.println("Results of " + SHUFFLE_COUNT
+ " consecutive perfect shuffles:"); …Run Code Online (Sandbox Code Playgroud) 我有一个类,我试图在每个空间(class1)中创建一个起始值为0的2D数组.我希望能够在另一个类(class2)中调用该数组,编辑这些值,然后将其存储回创建它的原始类(class1)中,以便在另一个类(class3)中调用,其值为从第二节改变了.
我知道必须有一些方法可以做到这一点,但如果它太复杂或不是一种有效的方式,我可以找到另一种方式做我想要完成的事情.如果你能给我一个很棒的例子.
现在我有一个小班.
public class Inventory {
public static int[][] inventory_1 = {
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
};
}
Run Code Online (Sandbox Code Playgroud)
我暂时使用它作为数组的占位符,但正如预期的那样,当从另一个类调用时,或者如果运行第二个类的新实例,它会将内容重置为原始数组.
Inventory inventorynew = new Inventory();
inventorynew.inventory_1 [0][0] = 1
Run Code Online (Sandbox Code Playgroud)
我实际上在做的是创建和库存系统,并根据每个地方的价值,它将确定库存中的内容.
关于原始类型如何在Java中工作,我有一个奇怪的问题.将ObjectA指定为ObjectB时使用对象时是这样的
Rectangle ObjectB = new Rectangle();
ObjectA = ObjectB;
Run Code Online (Sandbox Code Playgroud)
对ObjectA的任何调用现在都指向ObjectB的内存位置.但是,当使用整数或其他原始类型时,情况并非如此.例如
int x = 3;
int y = x;
int x = 5;
return y;
Run Code Online (Sandbox Code Playgroud)
当y被初始化时,y将返回3,即x的值.
我的问题是为什么对象的赋值在内存中创建引用,而原语会复制彼此的值?除了这个有用的事实,这是如何在Java中实现的?
我很感激能够让我更好地理解赋值在原始类型和对象之间如何工作的人.
我有以下java类,它具有实例变量属性作为声明类时传递的泛型类型.
当我尝试将属性传递给它作为方法参数时,如果我更改参数本身的值,它似乎不会更改其值.
码:
public class BST<T extends Comparable<T>, S>{
private Node<T, S> bstRoot;
public Node<T,S> getRoot() {
return bstRoot;
}
public void setRoot(Node<T,S> root) {
this.bstRoot = root;
}
public boolean put(T key, S value){
Node<T,S> node = new Node<T,S>(key, value);
return put(node, bstRoot);
}
private boolean put(Node<T,S> node, Node<T,S> root){
if(root == null){
root = node;
}
else{
if(root.compareTo(node) < 0){
put(node, root.getRightChild());
}
else{
put(node, root.getLeftChild());
}
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
当我执行以下操作时:
public class BSTTest {
public …Run Code Online (Sandbox Code Playgroud) 我试图制作一个代码来回答另一个人的问题......我出现了另一个问题.
这个想法是一个程序,要求你输入数字,以便对它们进行排序并从高到低打印出来.
这就是我的意思:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Input: ");
Integer input = new Integer(Integer.parseInt(scanner.nextLine()));
scanner.close();
Integer[] numbers = new Integer[input.toString().length()];
for (Integer value : numbers) {
while (input > 1) {
value = input % 10;
input /= 10;
}
}
Arrays.sort(numbers, Collections.reverseOrder());
int a = 0;
for (Integer value : numbers) {
if (a < numbers.toString().length()) {
System.out.print(value + ", ");
a++;
} else {
System.out.println(value + ".");
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,作为 …
以下关于Java的声明是假的吗?
在Java中,当一个类或对象的实例被指定为方法的参数时,正在创建该对象的副本
我知道Java中的函数是按值传递的,这意味着正在制作对象的副本?
但与此同时,如果java对象是引用并且你传递了一个引用,这不同于实际数据的副本不是吗?
如果你传递一个引用,当重新分配引用时,对象将被重新分配,使得Java传递引用不是按值传递的?
如你所见,我对此非常困惑
我正在尝试返回用户选择的文件.这很好.我可以在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) 我有两个列表(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”)