String[] m ={"a123","c321","a123","b456","c321","b456"}
想把数组m根据相同的元素拆分成3个新的数组:
{"a123","a123"} {"b456","b456"} {"c321","c321"}
请高人给下具体的实现代码,能给的全部分都给了哈!
JAVA的String 数组拆分问题,急急
答案:3 悬赏:40 手机版
解决时间 2021-04-14 04:24
- 提问者网友:泪痣哥哥
- 2021-04-13 19:56
最佳答案
- 五星知识达人网友:深街酒徒
- 2021-04-13 20:55
import java.util.*;
public class Test {
public static void main(String args[]) {
String[] m ={"a123","c321","a123","b456","c321","b456"} ;
String [][]retArr=getGroupArray(m) ;
for(int i=0;i<retArr.length;i++)
{
for(int j=0;j<retArr[i].length;j++)
{
System.out.print(retArr[i][j]+" ");
}
System.out.println();
}
}
public static String [][] getGroupArray(String [] m)
{
ArrayList value=new ArrayList();
ArrayList num=new ArrayList();
for(int i=0;i<m.length;i++)
{
int index=getIdx(value,m[i]);
if(index==-1)
{
value.add(m[i]);
num.add("1");
}
else
{
num.set(index, String.valueOf(Integer.parseInt((String)num. get(index))+1));
}
}
String [][]retArr=new String[value.size()] [];
for(int i=0;i<value.size();i++)
{
int count=Integer.parseInt((String)num.get(i));
retArr[i]=new String[count];
for(int j=0;j<count;j++)
{
retArr[i][j]=(String)value.get(i);
}
}
return retArr;
}
public static int getIdx(ArrayList arr,String value)
{
for(int i=0;i<arr.size();i++)
{
if((String)arr.get(i)==value)
{
return i;
}
}
return -1;
}
}
public class Test {
public static void main(String args[]) {
String[] m ={"a123","c321","a123","b456","c321","b456"} ;
String [][]retArr=getGroupArray(m) ;
for(int i=0;i<retArr.length;i++)
{
for(int j=0;j<retArr[i].length;j++)
{
System.out.print(retArr[i][j]+" ");
}
System.out.println();
}
}
public static String [][] getGroupArray(String [] m)
{
ArrayList value=new ArrayList();
ArrayList num=new ArrayList();
for(int i=0;i<m.length;i++)
{
int index=getIdx(value,m[i]);
if(index==-1)
{
value.add(m[i]);
num.add("1");
}
else
{
num.set(index, String.valueOf(Integer.parseInt((String)num. get(index))+1));
}
}
String [][]retArr=new String[value.size()] [];
for(int i=0;i<value.size();i++)
{
int count=Integer.parseInt((String)num.get(i));
retArr[i]=new String[count];
for(int j=0;j<count;j++)
{
retArr[i][j]=(String)value.get(i);
}
}
return retArr;
}
public static int getIdx(ArrayList arr,String value)
{
for(int i=0;i<arr.size();i++)
{
if((String)arr.get(i)==value)
{
return i;
}
}
return -1;
}
}
全部回答
- 1楼网友:旧脸谱
- 2021-04-13 23:10
package org.info.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Group {
public static void main(String[] args) {
String[] m = { "a123", "c321", "a123", "b456", "c321", "b456", "c321" };
Map<String, List<String>> map = new HashMap<String, List<String>>();
List<String> als = new ArrayList<String>();
for (int i = 0; i < m.length; i++) {
if (map.containsKey(m[i])) {
map.get(m[i]).add(m[i]);
} else {
als = new ArrayList<String>();
als.add(m[i]);
map.put(m[i], als);
}
}
for (Iterator iter = map.values().iterator(); iter.hasNext();) {
als = (List) iter.next();
Object[] objs = als.toArray();
for (int i = 0; i < objs.length; i++) {
System.out.print(objs[i] + "\t");
}
System.out.println();
}
}
}
- 2楼网友:零点过十分
- 2021-04-13 21:49
HashMap<String,LinkList<String>> counter = new HashMap<String,LinkList<String>>();
for(int i = 0 ; i < array.length ; i++){
if(counter.containsKey(array[i])){
counter.get(array[i]).add(i);
}else{
LinkList<String> x = new LinkList<String>();
x.add(array[i]);
coutner.put(array[i],x);
}
}
for (Iterator iter = counter .entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();//被拆分出来的数组
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯