---
title: "List<Object[]> 的用法"
author: "duyunfengsolo"
date: "2009-09-22T18:19:50Z"
tags: ["C", "C#", "C++", "DAO", "wicket"]
slug: "list-object"
draft: false
lastmod: "2009-09-22T18:19:50Z"
id: 474524
url: "https://solodu.iteye.com/blog/474524"
---

```
@SuppressWarnings("unchecked")
	public List<Object[]> getContentListByAppTitle(String username) {
		username = username.trim();
		List<Object[]> lists = getJpaTemplate()
			.find("select SUM(royaltyPrice),c.vendorId,c.appleId from Content c where c.appTitle='"
					+ username + "' group by c.appleId");
		
		return lists;
	}
```

 这是今天在写wicket的“view report”功能的时候所写的重要代码。

        这段代码起初我是用public List&lt;Content&gt;getContentListByAppTitle(String username) {}的方式，但是后来想了下明明就结果集就不是Content的对象，为什么要用List&lt;Content&gt;呢？ 如果该成这样的话就可以用：select c from Content c ....; 这样就可以用。现在必须要用SUM(royaltyPrice),所以不能用了。然后想了下就只能用数组了，

所以最后改成这样了List&lt;Object\[]&gt;了。

         在control层用这个dao层的这个方法的时候返回的是List&lt;Object&gt;类型的。所以这样来用：

```
private List<Object[]> contentList;

contentList = reportService.getContentListByAppTitle(sessionUser.getUsername());

if(contentList.size()>0){
	royaltyPrice = ((Double)contentList.get(0)[0]).floatValue();
	vendorId = Short.parseShort(contentList.get(0)[1].toString());
	appleId = Integer.parseInt(contentList.get(0)[2].toString());
}
```

 因为我的list里面就只有一条记录，所以用了get(0)来获取这条记录，但是这条记录本身是个Object\[]对象数组，所以又用到了get(0)\[0]和get(0)\[1]和get(0)\[2]，这样来获取每个记录。