传递二维ArrayList在类之间

huangapple go评论79阅读模式
英文:

Passing 2d arraylist between classes

问题

import java.util.ArrayList;

public class test {

    ArrayList<ArrayList<String>> stats = new ArrayList<ArrayList<String>>();

    public static void main(String[] args) {
        stats.get(0).add("one");
        System.out.println(stats.get(0).get(0));
    }
}
import java.util.ArrayList;

public class numbers {
    //add value
    //change value
}
英文:

How will i pass my 2d arraylist to number class, so that when i change it in there, my arraylist in test will also change and vice versa. like both class sharing the same variable;
i do pass by reference on C, but i dont know in java

import java.util.ArrayList;

public class test {

    ArrayList&lt;ArrayList&lt;String&gt;&gt; stats = new ArrayList&lt;ArrayList&lt;String&gt;&gt;();
    
    public static void main(String[] args)
    {
        stats.get(0).add(&quot;one&quot;);
        System.out.println(stats.get(0).get(0));
    }
}

I want this number class to be able to access and change the arraylist variable

import java.util.ArrayList;

public class numbers {
    //add value
    //change value
}

答案1

得分: 1

# TL;DR
*在我个人的观点中*,那些无需开发者额外努力就自动将数据结构作为引用传递的语言应被归类为按引用传递,因为引用过时的标准实际上对任何人都没有帮助。

# 对按值传递和按引用传递的抱怨
***从技术上讲,Java 是按值传递***

参考链接:https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value

然而,由于该值通常最终变成一个指针(除非传递的是原始类型),因此它实际上可以被视为按引用传递。

如今,几乎不存在按引用传递的语言,而且通常告诉某人一个语言是按值传递对回答他们的问题并没有太大帮助,事实上,在刚开始学习编程时,这可能会给他们带来错误的理解。

参考链接:https://stackoverflow.com/questions/2914181/what-are-there-any-languages-with-only-pass-by-reference

一般来说,大多数人想知道的是我的数据在哪里,它是如何传递的。由于像 Java 和 Python 这样的语言不允许你选择获取数据的指针(不,反射不算在内),这就引发了一个问题,如果我创建一个传递类型为 `Foo` 的函数,它会复制整个 `Foo` 对象内的值,还是仅仅引用同一个 `Foo` 对象。

以 C 和 Java 分别编写的这两个非常相似的程序为例。

C 版本:
```c
struct {
    int bar;
} Foo;

void editFoo(Foo foo) {
    foo.bar = 85;
}

Foo a;
a.bar = 6;

editFoo(a);
printf("bar: %d", a.bar);

Java 版本:

class Foo {
    int bar;
}

static void editFoo(Foo foo) {
    foo.bar = 85;
}

Foo a = new Foo();
a.bar = 6;

editFoo(a);
System.out.println("bar: " + a.bar);

C 版本会输出 6,表示函数未修改 a。要使其修改 a 中的数据,需要将函数修改为接受引用。然而,另一方面,Java 版本会输出 85,表示它在没有任何额外工作的情况下传递了一个引用。实际上,Java 并不像 C 那样提供按值传递的选项。如果我想传递一个完全独立但相同的 Foo 的副本,我需要创建一个 deepCopy 函数,并在每次传递对象时调用它。在我看来,这才是应该区分按值传递和按引用传递的标准。

回答你的问题...

你可以将其添加到构造函数中,或通过函数调用传递它。

public class Numbers {
    public static void modifyList(ArrayList<ArrayList<String>> list) {
        // 你也可以在这里访问它!
        list.get(0).add("two");
    }
}

// 在主函数的某处
Numbers.modifyList(stats);
System.out.println(list.get(0).get(0));
// 应输出 "two"

顺便说一下由于 `stats` 不是 `static`,但却在 `static` 上下文中使用你可能会遇到当前代码的问题
英文:

TL;DR

In my personal opinion, languages that automatically pass data structures as references without extra effort from the developer should be classified as pass-by-reference because pointing to an outdated standard doesn't really help anyone.

Rant about pass-by-value vs pass-by-reference

Technically Java is pass-by-value

Reference: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value

However, since that value often ends up being a pointer (Unless you are passing a primitive type) it might as well be pass by reference.

These days pass-by-reference languages are practically non-existent and more often than not telling someone a language is pass-by-value isn't very helpful to answering their question and may in fact give them the wrong idea when first learning how to code.

Reference: https://stackoverflow.com/questions/2914181/what-are-there-any-languages-with-only-pass-by-reference

Generally speaking, what most people want to know is where is my data and how is it passed. Since languages like Java and Python don't give you the option to take a pointer to data (No, reflection doesn't count), it raises the question of if I create a function that passes type Foo, will it copy values within the entire Foo object or just take a reference to the same foo object.

Take for example these two very similar programs in C and Java respectively.

struct {
    int bar;
} Foo;

void editFoo(Foo foo) {
    foo.bar = 85;

Foo a;
a.bar = 6;

editFoo(a);
printf(&quot;bar: %d&quot;, a.bar);
class Foo {
    int bar;
}

static void editFoo(Foo foo) {
    foo.bar = 85;
}

Foo a = new Foo();
a.bar = 6;

editFoo(a);
System.out.println(&quot;bar: &quot; + a.bar);

The C version prints 6, indicating that the function did not modify a. To make it mutate the data within a we would need to modify the function to specify that it takes a reference. However on the other hand, the Java version prints 85 indicating that it did pass a reference without any extra work on our part. In fact, Java doesn't give the option to do pass-by-value in the same way C does. If I want to pass a completely separate, but identical copy of Foo, I would need to create a deepCopy function and call it each time I passed the object. In my opinion, this is what should differentiate pass-by-value and pass-by-reference.

So answer your question...

You can just add it to the constructor or pass it through a function call.

public class Numbers {
    public static void modifyList(ArrayList&lt;ArrayList&lt;String&gt;&gt; list) {
        // You can access it here too!
        list.get(0).add(&quot;two&quot;);
    }
}

// Somewhere in main function
Numbers.modifyList(stats);
System.out.println(list.get(0).get(0));
// Should output &quot;two&quot;

On that note though, you may run into issues with your current code since stats isn't static but is being used in a static context.

huangapple
  • 本文由 发表于 2020年8月15日 07:59:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63421348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定