有两个ArrayList例如list1 = {1,2,3} 和list2 = {7,6,4,2}如何交换这两个列表.结果将是list1 = {7,6,4,2},list2 = {1,2,3}
我可以像这样实现:
public void swapList(ArrayList<Integer> list1, ArrayList<Integer> list2){
ArrayList<Integer> tmpList = list1;
list1 = list2;
list2 = tmpList;
}
Run Code Online (Sandbox Code Playgroud) 在Java中,包含适当对象的所有变量实际上都是引用(即指针).因此,使用这些对象作为参数的方法调用始终是"通过引用".调用修改对象状态的方法也会影响原始对象(在调用者端).
C++是不同的:这里的参数可以通过值传递或通过引用传递.在通过值传递的对象上调用mutator方法会使原始对象不受影响.(我想按值调用会创建对象的本地副本).
所以我对此的第一反应 - 从Java到C++ - 是:当使用对象作为参数时,总是使用指针.这给了我从Java期望的行为.
但是,如果一个人不需要修改方法体中的对象,也可以使用"按值调用".是否有人想要这样做?
I heard that in "c" that there are we can pass the arguments via "call by value" or "call by reference". But in one book it's mentioned that there are we can pass the arguments via both way but there is no "pass by reference" but I actually pass the mostly arguments by "pass by reference".
So why it is mentioned "does C even have "pass by reference"?
A detailed description will be greatly appreciated.
我正在尝试为我一直在研究的BST结构实现一个remove方法.以下是包含find,insert和remove方法的代码:
public class BST {
BSTNode root = new BSTNode("root");
public void insert(BSTNode root, String title){
if(root.title!=null){
if(title==root.title){
//return already in the catalog
}
else if(title.compareTo(root.title)<0){
if(root.leftChild==null){
root.leftChild = new BSTNode(title);
}
else{
insert(root.leftChild,title);
}
}
else if(title.compareTo(root.title)>0){
if(root.rightChild==null){
root.rightChild = new BSTNode(title);
}
else{
insert(root.rightChild,title);
}
}
}
}
public void find(BSTNode root, String title){
if(root!= null){
if(title==root.title){
//return(true);
}
else if(title.compareTo(root.title)<0){
find(root.leftChild, title);
}
else{
find(root.rightChild, title);
}
}
else{
//return false;
}
}
public void remove(BSTNode root, …Run Code Online (Sandbox Code Playgroud) 我被困在一件简单的事情上.我有一系列名为"标签"的布尔值.能够通过布尔值访问数组的每个元素对我来说很重要:
public boolean energetic, calming, happy, sad;
public boolean[] trackTags = {energetic, calming, happy, sad};
Run Code Online (Sandbox Code Playgroud)
我传入并为trackTags数组分配布尔值(让我们说[true,true,true,false].所以,当我调用trackTags [0]时,我得到"真实".但是,当我打印"精力充沛"时 -它应该与trackTags [0]相同,值总是为false.我知道布尔值初始化为false,但是当我在trackTags数组中切换某些值为"true"时,命名元素是否也不会改变?
第二个问题:对于我与布尔变量名称交互的好方法是什么?具体来说,如果我传入一个String [] [happy,sad],然后只想切换对应于我的String数组中名称的boolean []值,这是否可行?我在循环遍历两个数组的元素时没有问题,但我显然无法将字符串与布尔值进行比较.
总而言之,是否有更好的方式与布尔名称进行交互?我真的很困惑.
我曾经认为Java支持传递值和passby参考,但我遇到了许多讨论,如
如果java只支持按值传递
怎么做java.util.Array.sort()或Collections.sort(unsortList)有效?
int iArr[] = {2, 1, 9, 6, 4};// sorting array
Arrays.sort(iArr);
System.out.println("The sorted int array is:");
for (int number : iArr) {
System.out.println("Number = " + number);
}
Run Code Online (Sandbox Code Playgroud)
更新: 传递引用的内容(按值)实际上是什么意思?它与通过C或C++中的数组的引用行为有什么不同?
更新:
如果我错了,请纠正我.在C中,我们在通过引用传递时传递变量的地址.在Java中,我们将引用传递给对象(或值).只要方法中的变量指向Object,对象的值就会随着变量而变化.调用的方法.
没有Object或reference的副本!我只能看到2个不同的变量指向同一个Object,而不是通过引用传递.在C++中的类似传递引用两个不同的变量指向同一个地址.
根据本教程,Python使用"Pass By Reference".
然后他们继续给出以下示例.在这个"通过参考传递"的星球上?它看起来像是一个明确的"通过价值"案例给我.
思考?
def changeme( mylist ):
mylist = [1,2,3,4];
print "Values inside the function: ", mylist
return
mylist = [10,20,30];
changeme( mylist );
print "Values outside the function: ", mylist
Run Code Online (Sandbox Code Playgroud)
参数mylist是函数changeme的本地.更改函数中的mylist不会影响mylist.该函数什么都不做,最后会产生以下结果:
# Values inside the function: [1, 2, 3, 4]
# Values outside the function: [10, 20, 30]
Run Code Online (Sandbox Code Playgroud) 我必须得到一些直接的东西.请告诉我我是否正确:
String a;
String b;
Run Code Online (Sandbox Code Playgroud)
a = b表示b取a的值?
和
b = a表示a取b的值?
我对这个很尴尬,我很感激解释.
在 C++ 中我可能会这样做:
sometype var = somevalue;
mutate(var);
// var is now someothervalue
void mutate(sometype &a) {
a = someothervalue;
}
Run Code Online (Sandbox Code Playgroud)
Java中有类似的东西吗?
我正在尝试完成类似的事情:
Customer a;
public static void main(String[] args) {
a = newCustomer("Charles");
Customer b = null;
mutate(b);
System.out.println(b.getName()); // NullPointerException, expected "Charles"
}
void mutate(Customer c) {
c = a;
}
Run Code Online (Sandbox Code Playgroud)
如果Customer是可变的,为什么会产生 NullPointerException?
我正在从Udemy学习Java ,完整的java masterclass.对于挑战"抽象类挑战",叙述者说我应该在抽象类中创建2个引用和1个值.
在解决方案中,这是结果:
public abstract class ListItem {
// references
protected ListItem rightLink = null;
protected ListItem leftLink = null;
// value
protected Object object;
}
Run Code Online (Sandbox Code Playgroud)
什么使引用成为引用而不是值?
它们都以protected开头,然后我们有类型,然后我们有变量名.唯一的区别是引用已赋值null.
但如果要举例如:
private int myNumber = 10;
Run Code Online (Sandbox Code Playgroud)
上面不称为引用,它被称为类型为int的变量myNumber,值为10.