int[] a = {1,1,1,2,2,3,4,4,4,4,4,4};写一个方法根据a[]里面的值撤分成多个int[]
结果:
b[]={1,1,1};
c[]={2,2};
d[]={3};
e[]={4,4,4,4,4,4};
结果:
b[]={1,1,1};
c[]={2,2};
d[]={3};
e[]={4,4,4,4,4,4};
解决方案 »
- 新手求解惑...谢了......
- Java 动画的问题 求高手指点
- jComboBox遇到的问题
- 第一次写GUI但没看到相应结果,请高手帮忙
- 实现:在键盘上输入一个数字序列并存储在Vector中,最后在屏幕上打印出每位数字相加的结果。编译可以通过,最后出不来结果。
- 如何知道一个字符串是否包含dom4j解析器所不能解析的字符?
- 我实在么的分给大家了.有关310-055的问题
- 安装了1.4.02_03的j2sdk,请问如何查看每个类包的详细信息
- 反射调用setter/getter方法的一个小问题
- 请问各位有关Assertion机制的两个问题,马上给分!!!
- 父类调用了子类的方法???
- 寻求java资料
class Ju { public static void main(String[] args) {
int[] b = {1,1,1,2,2,3,4,4,4,4,4,4};
int []a=new int[b.length+1];
for (int i = 0; i < b.length; i++) {
a[i]=b[i];
}
a[b.length]=b[b.length-1]+1;
int count=1;
int temp=a[0];
for (int i = 1; i < a.length; i++) {
if(a[i]==temp){
count++;
}else{
int []ab=new int[count];
for (int j = 0; j < ab.length; j++) {
ab[j]=temp;
System.out.print(ab[j]+" ");
}
temp=a[i];
count=1;
System.out.println("");
}
}
}
}一个很麻烦,很烂的写法.
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;public class Test
{
public static void main(String[] args)
{
LinkedHashMap<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();
int[] a = {1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 4, 4};
for(int n: a)
{
if(map.containsKey(n))
{
map.put(n, map.get(n) + 1);
}
else
{
map.put(n, 1);
}
}
List<List<Integer>> list = new ArrayList<List<Integer>>();
List<Integer> subList;
for(int key: map.keySet())
{
subList = new ArrayList<Integer>();
for(int i = 0; i < map.get(key); i++)
{
subList.add(key);
}
list.add(subList);
}
for(List<Integer> outList: list)
{
for(int num: outList)
{
System.out.print(num + " ");
}
System.out.println();
}
/*运行结果
1 1 1
2 2
3
4 4 4 4 4 4
*/
}
}
用一个变量temp标记,一开始设置为a[0],也就是1.循环往后比较,并记录temp值出现的次数,当比较到2的时候,这时候确定1已经数完了,得到第一个数组.然后设置temp为2,继续往下数....
一开始复制数组主要是为了设置哨兵节点方便处理.
if (a == null) return null;
if (a.length == 0) return new int[0][];
a = Arrays.copyOf(a, a.length + 1); //准备好 a 的副本
Arrays.sort(a, 0, a.length - 1); //先排好序
a[a.length - 1] = a[a.length - 2] + 1;
List<int[]> list = new LinkedList<int[]>();
int len = a.length - 1;
for (int i = 0, j = 0; i < len;) {
if (a[i] == a[++i]) continue;
list.add(Arrays.copyOfRange(a, j, i));
j = i;
}
return list.toArray(new int[list.size()][]);
} public static void main(String[] args) {
int[] a = {1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 4, 4};
int[][] r = splitArray(a);
for (int[] ia : r) {
for (int i : ia) {
System.out.print(i + " ");
}
System.out.println();
}
}}
public static void main(String[] args)
{
int[] a = {1,1,1,2,2,3,4,4,4,4,4,4}; List tmpList=splitArray(a);
for(Iterator i=tmpList.iterator();i.hasNext();)
{
int[] tmpArr=(int[])i.next();
for(int j=0;j<tmpArr.length;j++)
{
System.out.print(tmpArr[j]);
}
System.out.println();
}
}public static List splitArray(int[] orgArr)
{
List returnList=new ArrayList();
int i=0;
while(i<orgArr.length)
{
int iValue=orgArr[i];
int j=i+1;
for(;j<orgArr.length;j++)
{
if(orgArr[j]!=iValue)
break;
}
int[] tmpArr=new int[j-i];
System.arraycopy(orgArr, i, tmpArr, 0, tmpArr.length);
returnList.add(tmpArr);
i=j;
} return returnList;
}
public class Test{
private static void test(int[] a){
ArrayList al = new ArrayList();
int temp = 0;
int breakout = a.length;
int i = 0;
int j;
while(true){
temp = a[i];
for(;i < a.length;i++){
if(temp == a[i])
al.add(a[i]);
else break;
}
for(j = 0;j < al.size();j++){
System.out.print(al.get(j)+" ");
}
System.out.println();
al.clear();
if(breakout == i) break;
}
}
public static void main(String[] args){
int[] a = {1,1,1,2,2,3,4,4,4,4,4,4};
test(a);
}
}
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;public class ArraySpliter { /**
* 利用了 JRE1.6 的新方法。
* @param a 待分割的一维数组
* @return 分割好的二维数组
*/
public static int[][] splitArray(int[] a) {
if (a == null) return null;
if (a.length == 0) return new int[0][];
a = Arrays.copyOf(a, a.length + 1); //准备好 a 的副本
Arrays.sort(a, 0, a.length - 1); //先排好序
a[a.length - 1] = a[a.length - 2] + 1;
List<int[]> list = new LinkedList<int[]>();
int len = a.length - 1;
for (int i = 0, j = 0; i < len;) {
if (a[i] == a[++i]) continue;
list.add(Arrays.copyOfRange(a, j, i));
j = i;
}
return list.toArray(new int[list.size()][]);
} /**
* 兼容 JRE1.5。
* @param a 待分割的一维数组
* @return 分割好的二维数组
*/
public static int[][] splitIntArray(int[] a) {
if (a == null) return null;
if (a.length == 0) return new int[0][];
List<int[]> list = new ArrayList<int[]>(a.length); //作最坏的打算
System.arraycopy(a, 0, a = new int[a.length + 1], 0, a.length - 1); //准备好 a 的副本
Arrays.sort(a, 0, a.length - 1); //先排好序
a[a.length - 1] = a[a.length - 2] + 1;
int len = a.length - 1;
for (int i = 0, j = 0; i < len;) {
if (a[i] == a[++i]) continue;
int[] ia = new int[i - j];
System.arraycopy(a, j, ia, 0, ia.length);
list.add(ia);
j = i;
}
return list.toArray(new int[list.size()][]);
} public static void main(String[] args) {
int[] a = {0, 1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 4, 4, -3};
int[][] r = splitIntArray(a);
for (int[] ia : r) {
for (int i : ia) {
System.out.print(i + " ");
}
System.out.println();
}
}}输出结果:-3
0
1 1 1
2 2
3
4 4 4 4 4 4
/**
*
*/
package com.test;import java.util.HashMap;
import java.util.Iterator;
/**
* @author zhameng
*
*/
public class SplitArray {
/**
* 功能:把一个整型数组根据内容拆分成N个内容相同的数组
* 创建时间:2008年7月24日
*
* @param a 整型数组
*/
public static void getArray(int[] a){
HashMap hashmap = new HashMap();
for(int i = 0; i < a.length; i++){
if(hashmap.get(a[i]+"")==null){ // 如果值不存在于数组中,增加该值
hashmap.put(a[i]+"", "0");
}else{ // 如果值不存在于数组中,增加该值的个数
int count = Integer.parseInt((String)hashmap.get(a[i]+""))+1;
hashmap.put(a[i]+"", count+"");
}
}
// 一下步骤用于显示,可以转化为题目所需要的数组
for(Iterator it = hashmap.keySet().iterator(); it.hasNext();){
String key = (String)it.next();
for(int i = 0; i <= Integer.parseInt((String)hashmap.get(key)); i++){
System.out.print(key+" ");
}
System.out.println("");
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
int[] a = {1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 4, 4};
getArray(a);
}}
3
2 2
1 1 1
4 4 4 4 4 4 虽然要求得到的是数组集,不过在不知道数组长度的时候初始化很麻烦,所以我绕了点路,用hashmap来获得值,可能方法不是很好,不过我想毕竟是一条路。
用一个变量temp标记,一开始设置为a[0],也就是1.循环往后比较,并记录temp值出现的次数,当比较到2的时候,这时候确定1已经数完了,得到第一个数组.然后设置temp为2,继续往下数....
一开始复制数组主要是为了设置哨兵节点方便处理.
具体思想就是这么一个思路