由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - ArraList question
相关主题
Java练习题 12is access to int[] faster than List?
问一个 java generic问题Unmodifiable List concurrent access 问题
[转载] javascript怎么访问JSP里的arraylist?问个 Generic 的问题
请帮忙看看这个编译错误web service returns HashMap that contains multiple ArrayList
List, LinkedList and VectorDesign question --- hibernate
Generic type cast warningjava的volatile
ArrayList and Link listSingleton Session Bean
immutable listjava compilation question
相关话题的讨论汇总
话题: arraylist话题: list1话题: new话题: list话题: arralist
进入Java版参与讨论
1 (共1页)
P*P
发帖数: 36
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);
q*c
发帖数: 9453
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");

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

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

q*c
发帖数: 9453
4
用两个 arraylist

【在 P*P 的大作中提到】
: 多谢.那我用什么才能实现[[a,b][A,B]]呢?关键我实现不知道size会有多大.
A**o
发帖数: 1550
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会有多大.
P*P
发帖数: 36
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);

o***g
发帖数: 2784
7
什么叫要用几个?

谢.

【在 P*P 的大作中提到】
: 我的意思是如果事先不知道要用几个arraylist,比如在loop里,那应该怎么实现呢?多谢.
A**o
发帖数: 1550
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里,那应该怎么实现呢?多谢.
P*P
发帖数: 36
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啦?
:
: 谢.

A**o
发帖数: 1550
10
大过节的还写代码,不容易啊。

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

相关主题
Generic type cast warningis access to int[] faster than List?
ArrayList and Link listUnmodifiable List concurrent access 问题
immutable list问个 Generic 的问题
进入Java版参与讨论
P*P
发帖数: 36
11
别提了,都是眼泪.
混口饭吃真难啊.

【在 A**o 的大作中提到】
: 大过节的还写代码,不容易啊。
r*****l
发帖数: 2859
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");

m******t
发帖数: 2416
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());

r*****l
发帖数: 2859
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-)

m******t
发帖数: 2416
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.

r*****l
发帖数: 2859
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-)

m******t
发帖数: 2416
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"

r*****l
发帖数: 2859
18
Even I don't like to solution. I don't like to use clone()
method.
1 (共1页)
进入Java版参与讨论
相关主题
java compilation questionList, LinkedList and Vector
help on this scope questionGeneric type cast warning
Help with Tomcat for Eclipse v1.03ArrayList and Link list
ant javac error in eclipse 3.0?immutable list
Java练习题 12is access to int[] faster than List?
问一个 java generic问题Unmodifiable List concurrent access 问题
[转载] javascript怎么访问JSP里的arraylist?问个 Generic 的问题
请帮忙看看这个编译错误web service returns HashMap that contains multiple ArrayList
相关话题的讨论汇总
话题: arraylist话题: list1话题: new话题: list话题: arralist