Created
January 19, 2010 16:50
-
-
Save Kanasansoft/281077 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kanasansoft.MultidimensionalArrayLists; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class MultidimensionalArrayLists<E>{ | |
private static final long serialVersionUID=1L; | |
private int[] indexes_=null; | |
private E data_=null; | |
private ArrayList<MultidimensionalArrayLists<E>> children_=new ArrayList<MultidimensionalArrayLists<E>>(); | |
public MultidimensionalArrayLists(int... indexes){ | |
if(indexes.length==0){ | |
return; | |
} | |
if(indexes[0]<0){ | |
throw new IllegalArgumentException(); | |
} | |
int[] childIndexes=Arrays.copyOfRange(indexes,1,indexes.length); | |
ArrayList<MultidimensionalArrayLists<E>> children = new ArrayList<MultidimensionalArrayLists<E>>(); | |
for(int i=0;i<indexes[0];i++){ | |
children.add(new MultidimensionalArrayLists<E>(childIndexes)); | |
} | |
this.indexes_=indexes; | |
this.children_=children; | |
} | |
public MultidimensionalArrayLists(List<E> arrayList,int... indexes){ | |
if(indexes.length==0){ | |
this.data_=arrayList.get(0); | |
return; | |
} | |
if(indexes[0]<0){ | |
throw new IllegalArgumentException(); | |
} | |
int length=1; | |
for(int i=0;i<indexes.length;i++){ | |
length*=indexes[i]; | |
} | |
if(arrayList.size()!=length){ | |
throw new IllegalArgumentException(); | |
} | |
int[] childIndexes=Arrays.copyOfRange(indexes,1,indexes.length); | |
ArrayList<MultidimensionalArrayLists<E>> children = new ArrayList<MultidimensionalArrayLists<E>>(); | |
for(int i=0;i<indexes[0];i++){ | |
List<E> childrenArrayList = arrayList.subList(length/indexes[0]*i,length/indexes[0]*(i+1)); | |
children.add(new MultidimensionalArrayLists<E>(childrenArrayList,childIndexes)); | |
} | |
this.indexes_=indexes; | |
this.children_=children; | |
} | |
public void put(E value,int... indexes){ | |
if(this.indexes_==null&&indexes.length==0){ | |
this.data_=value; | |
return; | |
} | |
if(this.indexes_.length!=indexes.length){ | |
throw new IllegalArgumentException(); | |
} | |
int length=this.indexes_.length; | |
for(int i=0;i<length;i++){ | |
if(this.indexes_[i]<=indexes[i]){ | |
throw new IndexOutOfBoundsException(); | |
} | |
} | |
int[] childIndexes=Arrays.copyOfRange(indexes,1,indexes.length); | |
this.children_.get(indexes[0]).put(value,childIndexes); | |
} | |
public E get(int... indexes){ | |
if(this.indexes_==null&&indexes.length==0){ | |
return this.data_; | |
} | |
if(this.indexes_.length!=indexes.length){ | |
throw new IllegalArgumentException(); | |
} | |
int length=this.indexes_.length; | |
for(int i=0;i<length;i++){ | |
if(this.indexes_[i]<=indexes[i]){ | |
throw new IndexOutOfBoundsException(); | |
} | |
} | |
int[] childIndexes=Arrays.copyOfRange(indexes,1,indexes.length); | |
return this.children_.get(indexes[0]).get(childIndexes); | |
} | |
public int[] getIndexes(){ | |
int length=this.indexes_.length; | |
int[] indexes=new int[length]; | |
for(int i=0;i<length;i++){ | |
indexes[i]=this.indexes_[i]; | |
} | |
return indexes; | |
} | |
public ArrayList<E> getFlatten(){ | |
ArrayList<E> arrayList=new ArrayList<E>(); | |
if(this.indexes_.length==1){ | |
for(int i=0;i<this.indexes_[0];i++){ | |
arrayList.add(this.children_.get(i).get()); | |
} | |
}else{ | |
for(int i=0;i<this.indexes_[0];i++){ | |
arrayList.addAll(this.children_.get(i).getFlatten()); | |
} | |
} | |
return arrayList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment