一维List转换为二维数组

本文详细介绍了如何在Java中将一维List转换为二维数组,包括使用ArrayList和for循环实现的方法,以及示例代码,帮助读者理解数据结构转换的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.util;

import java.util.Comparator;

public class ComparatorTime implements Comparator<String> {
	@Override
	public int compare(String arg0, String arg1) {
		int flag = arg0.compareTo(arg1);
		return flag;
	}

	public static void main(String[] args) {
		String a ="2014";
		String b = "2013Q3";
		System.out.println(a.compareTo(b));
	}
}

package com.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.A;

public class Util {
	public static List<A> lists = null;
	public static void main(String[] args) {
		Map<String,List<A>> timeMap = new HashMap<String,List<A>>();
		List<A> as = getList();
		List<String> staticTimeList = new ArrayList<String>();
		for(A a:as){
			String staticTime = a.getStaticTime();
			List<A> alist = timeMap.get(a.getStaticTime());
			if(alist==null){
				alist = new ArrayList<A>();
				timeMap.put(staticTime, alist);
				staticTimeList.add(staticTime);
			}
			alist.add(a);
		}
		
		sort(staticTimeList);
		
		Map<Integer,List<A>> dataMap = new HashMap<Integer,List<A>>();
		List<Integer> indexIDList = new ArrayList<Integer>();
		for(A a:as){
			Integer indexID = a.getIndexID();
			List<A> alist = dataMap.get(indexID);
			if(alist==null){
				alist = new ArrayList<A>();
				dataMap.put(indexID, alist);
				indexIDList.add(indexID);
			}
			alist.add(a);
		}
		int indexSize = indexIDList.size();
		int timeSize = staticTimeList.size();
		String[] timeArr = list2array(staticTimeList);
		Double[][] data = new Double[indexSize][timeSize]; 
		for(int i=0;i<indexSize;i++){
			for(int t=0;t<timeSize;t++){
				data = null;
			}
		}
		int i=0;
		for(Integer indexID:indexIDList){
			List<A> alist = dataMap.get(indexID);
			for(int t=0;t<timeSize;){
				for(A a:alist){
					if(a.getStaticTime().equals(timeArr[t])){
						data[i][t] = a.getValue();
						continue;
					}
				}
				t++;
			}
			i++;
		}
		
		
		System.out.println("end");
	}
	
	public static List<A> getList(){
		if(lists==null){
			lists = new ArrayList<A>();
			for(int i=2;i<102;i++){
				A a = new A(i%10, "200"+(i%9) , i);
				lists.add(a);	
			}
		}
		
		return lists;
	}
	
	public static List<String> sort(List<String> staticTimeList){
		ComparatorTime comparator = new ComparatorTime();
		Collections.sort(staticTimeList, comparator);
		return staticTimeList;
	}
	
	public static String[] list2array(List<String> list){
		String[] arr = (String[])list.toArray(new String[list.size()]);
		return arr;
	}
}



package com.demo;

public class A {
	int indexID;
	String staticTime;
	double value;
	public A(int indexID, String staticTime, double value) {
		super();
		this.indexID = indexID;
		this.staticTime = staticTime;
		this.value = value;
	}
	public int getIndexID() {
		return indexID;
	}
	public void setIndexID(int indexID) {
		this.indexID = indexID;
	}
	public String getStaticTime() {
		return staticTime;
	}
	public void setStaticTime(String staticTime) {
		this.staticTime = staticTime;
	}
	public double getValue() {
		return value;
	}
	public void setValue(double value) {
		this.value = value;
	}
	
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值