现在的位置: 首页 > 综合 > 正文

数组与集合之间的转换

2013年10月15日 ⁄ 综合 ⁄ 共 1023字 ⁄ 字号 评论关闭

JAVA中二维数组转换成集合

public class MyTest extends JFrame{

	public void MyTest(){
		String [][] str = new String [5][5]; 
		str[0][0] ="1";
		str[1][1] ="2";
		str[2][2] ="3";
		str[3][3] ="4";
		str[4][4] ="5";
		List list = new ArrayList();
		for(int i=0; i<5; i++){
			for(int j=0;j<5; j++){
				list.add(str[i][j]);
			}
		}
		
		for(int k = 0; k<list.size(); k++){
			System.out.print(list.get(k)+"  ");
		} 
	}
	    public static void main(String[] args) {
	       new MyTest().MyTest();
	    }

}

private Product[] products={

          product(categoryid,string,note);

             product(categoryid,string,note);

             product(categoryid,string,note)

}

List<Product> ps=new ArrayList<Product>();

for(Product  p : products)
{

ps.add(p);

}

List<Category> categorys=newArrayList<Category>();

      categorys.add(new Category(categoryid,"string"))

      categorys.add(new Category(categoryid,"string"))

      categorys.add(new Category(categoryid,"string"))

      categorys.add(new Category(categoryid,"string"))

java中集合与数组的类型转换  

正确做法:

if(productList.size()==0)
  {
   return null;
  }
  else
  {
   ProductBean [] p=new ProductBean[productList.size()];
   productList.toArray(p);
   return p;
  }

抱歉!评论已关闭.