avatar
ArraList question# Java - 爪哇娇娃
P*P
1
为什么list的结果是[[A,B][A,B]],而不是[[a,b][A,B]]
请各位高手指点,谢谢
ArrayList list1 = new ArrayList();
ArrayList list = new ArrayList();
list1.add("a");
list1.add("b");
list.add(list1);

list1.set(0, "A");
list1.set(1, "B");
list.add(list1);
avatar
q*c
2
因为 list1 是个指针, 你改的时候 list 就被改了。
、你就是把一个指针在 list 里面加了两次而已。

【在 P*P 的大作中提到】
: 为什么list的结果是[[A,B][A,B]],而不是[[a,b][A,B]]
: 请各位高手指点,谢谢
: ArrayList list1 = new ArrayList();
: ArrayList list = new ArrayList();
: list1.add("a");
: list1.add("b");
: list.add(list1);
:
: list1.set(0, "A");
: list1.set(1, "B");

avatar
P*P
3
多谢.那我用什么才能实现[[a,b][A,B]]呢?关键我实现不知道size会有多大.

【在 q*c 的大作中提到】
: 因为 list1 是个指针, 你改的时候 list 就被改了。
: 、你就是把一个指针在 list 里面加了两次而已。

avatar
q*c
4
用两个 arraylist

【在 P*P 的大作中提到】
: 多谢.那我用什么才能实现[[a,b][A,B]]呢?关键我实现不知道size会有多大.
avatar
A*o
5
ArrayList listlist = new ArrayList();
ArrayList list1 = new ArrayList();
list1.add("a");
list1.add("b");
listlist.add(list1);
ArrayList list2 = new ArrayList();
list2.set(0, "A");
list2.set(1, "B");
listlist.add(list2);

【在 P*P 的大作中提到】
: 多谢.那我用什么才能实现[[a,b][A,B]]呢?关键我实现不知道size会有多大.
avatar
P*P
6
我的意思是如果事先不知道要用几个arraylist,比如在loop里,那应该怎么实现呢?多谢.

【在 A**o 的大作中提到】
: ArrayList listlist = new ArrayList();
: ArrayList list1 = new ArrayList();
: list1.add("a");
: list1.add("b");
: listlist.add(list1);
: ArrayList list2 = new ArrayList();
: list2.set(0, "A");
: list2.set(1, "B");
: listlist.add(list2);

avatar
o*g
7
什么叫要用几个?

谢.

【在 P*P 的大作中提到】
: 我的意思是如果事先不知道要用几个arraylist,比如在loop里,那应该怎么实现呢?多谢.
avatar
A*o
8
ArrayList listlist = new ArrayList();
for( int i = 0; i < whatever; i++ ) {
ArrayList list = new ArrayList();
list.add(getA(i));
list.add(getB(i));
listlist.add(list);
}
您为啥不写php啦?

谢.

【在 P*P 的大作中提到】
: 我的意思是如果事先不知道要用几个arraylist,比如在loop里,那应该怎么实现呢?多谢.
avatar
P*P
9
嘿嘿,本来也就没写PHP.随便起着玩的.
多谢解答.

【在 A**o 的大作中提到】
: ArrayList listlist = new ArrayList();
: for( int i = 0; i < whatever; i++ ) {
: ArrayList list = new ArrayList();
: list.add(getA(i));
: list.add(getB(i));
: listlist.add(list);
: }
: 您为啥不写php啦?
:
: 谢.

avatar
A*o
10
大过节的还写代码,不容易啊。

【在 P*P 的大作中提到】
: 嘿嘿,本来也就没写PHP.随便起着玩的.
: 多谢解答.

avatar
P*P
11
别提了,都是眼泪.
混口饭吃真难啊.

【在 A**o 的大作中提到】
: 大过节的还写代码,不容易啊。
avatar
r*l
12

list.add(list1.clone());

【在 P*P 的大作中提到】
: 为什么list的结果是[[A,B][A,B]],而不是[[a,b][A,B]]
: 请各位高手指点,谢谢
: ArrayList list1 = new ArrayList();
: ArrayList list = new ArrayList();
: list1.add("a");
: list1.add("b");
: list.add(list1);
:
: list1.set(0, "A");
: list1.set(1, "B");

avatar
m*t
13

This isn't going to work if the code that follows still references list1.
Just do it "honest and square" like Amao's code... 8-)

【在 r*****l 的大作中提到】
:
: list.add(list1.clone());

avatar
r*l
14
I don't post if my code does not compile or is not tested.
Before you say "This isn't going to work", compile and run
the code yourself.

【在 m******t 的大作中提到】
:
: This isn't going to work if the code that follows still references list1.
: Just do it "honest and square" like Amao's code... 8-)

avatar
m*t
15
I didn't _just_ say "this isn't going to work". I qualified it - in the "if"
part, which sort of comes right after. 8-)

【在 r*****l 的大作中提到】
: I don't post if my code does not compile or is not tested.
: Before you say "This isn't going to work", compile and run
: the code yourself.

avatar
r*l
16
After doing list1.clone() the link between the newly added
element and list1 was broken.
So your 'if' does not hold. If you really understand my
reply, there should have no 'if' in your response.

if"

【在 m******t 的大作中提到】
: I didn't _just_ say "this isn't going to work". I qualified it - in the "if"
: part, which sort of comes right after. 8-)

avatar
m*t
17

OK, there is no need to get aggressive. 8-)
The way you posted your change (see below), it was not immediately obvious
to me that you meant to replace the line above. I thought you wanted to
insert it.
list.add(list1.clone());

【在 r*****l 的大作中提到】
: After doing list1.clone() the link between the newly added
: element and list1 was broken.
: So your 'if' does not hold. If you really understand my
: reply, there should have no 'if' in your response.
:
: if"

avatar
r*l
18
Even I don't like to solution. I don't like to use clone()
method.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。