java(13)List-----List嵌套

我的需求是想一次性传递多个list。

现在在测试可以如何实现。查看test4:list<list<String>>的初始化

在我不同测试和思路中,对list传递有几个不同的猜测和实验。

test1:测试两个list的话,一个list存数据,那么另一个list是否可以存第一个list的首地址。

首先这个问题是可以实现的。

List<String> tep = new ArrayList<String>();

List<String> ls = new ArrayList<String>();

tep.add("A");

tep.add("B");

tep.add("C");

ls.add(tep.get(0));

System.out.println(ls.get(0));

这么是可以访问到数据A的。

test2:但是我的问题是,我想用一个链表ls,他其中存的都是其他链表的首地址,这样我就可以通过传递ls那个List去传递所有的链表了。当然,测试后发现并不可以,其实问题出在了s.get(index_1).get(index_2);是不可以的。就是我是能访问tep的第一个数据A,但是我不能连着两个get得到B和C;如果我用String也仍然是不可以的。

 

test3:如果我们这么声明List<List<String>>的话,可以吗?怎么定义,怎么初始化啊?

定义:如下的两种定义方式都是不会报错的。

    

如何初始化和使用呢?

    一层一层赋值就可以了,因此也实现了多个list的传值。

输出结果:

 

test4: 测试两个list放进入是否成立。因此多个list的传递是可以放在一个List中,然后传递List<List<String>>过去的。

 

输出结果:

接受该参数的函数,只需要用同样的变量接受。那他怎么使用内层的List呢?因此无论基层都是可以使用的。

 

 

 

仿某团list嵌套list\ package com.zl.listview; import java.util.ArrayList; import java.util.HashMap; import com.kjy.kjylistview.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.Toast; /** * @author zl * @time 2014年12月24日15:37:18 */ public class QianTaoListviewActivity extends Activity { private ListView listView; private ArrayList<HashMap<String, Object>> parentList, childList; private ParentAdapter parentAdapter; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_qiantao); init(); } private void init() { listView = (ListView) findViewById(R.id.qiantao_lv); getList(); } private void getList() { parentList = new ArrayList<HashMap<String, Object>>(); for (int i = 1; i < 10; i++) { childList = new ArrayList<HashMap<String, Object>>(); for (int j = i < 7 ? i : 7; j < 9; j++) { HashMap<String, Object> ParentMap = new HashMap<String, Object>(); ParentMap.put("parent_title", "三木铁板烧" + i); ParentMap.put("parent_address", "8." + i); ParentMap.put("parent_distance", "1" + i); ParentMap.put("parent_distance1", "已售123" + i); childList.add(ParentMap); } HashMap<String, Object> map = new HashMap<String, Object>(); map.put("child_item_title", "九州八拖二火锅" + i); map.put("child_item_price_old", "4." + i + "分"); map.put("child_item_price_score", "(100人)"); map.put("parent_lv", childList); parentList.add(map); } parentAdapter = new ParentAdapter(parentList, QianTaoListviewActivity.this); listView.setAdapter(parentAdapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Intent intent = new Intent(QianTaoListviewActivity.this, Activity_1.class); Toast.makeText(QianTaoListviewActivity.this, "第" + arg2 + "个条目", Toast.LENGTH_LONG).show(); startActivity(intent); } }); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值