如题:我现在需要用算法来实现下面的功能
现在有一个这样的字符串String test = "1,2,1,3,2,4,5,3,2";
我想把他变成这样的String[] test1=[1,1];
String[] test1=[2,2,2];
String[] test3=[3,3];
String[] test4=[4];
String[] test5=[5];
也就是说把这个字符串里相同的数字放到一个数组里。动态生成N个数组。
现在有一个这样的字符串String test = "1,2,1,3,2,4,5,3,2";
我想把他变成这样的String[] test1=[1,1];
String[] test1=[2,2,2];
String[] test3=[3,3];
String[] test4=[4];
String[] test5=[5];
也就是说把这个字符串里相同的数字放到一个数组里。动态生成N个数组。
String[] values = test.split(",");
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (String value : values) {
if (map.containsKey(value)) {
int count = map.get(value);
map.put(value, ++count);
} else {
map.put(value, 1);
}
}
for (Entry<String, Integer> entry : map.entrySet()) {
String value = entry.getKey();
int count = entry.getValue();
String[] arr = new String[count];
for (int i = 0; i < count; i ++) {
arr[i] = value;
System.out.print(value + ", ");
}
System.out.println();
}
先转化成一个字符串数组,用equals是不是相同,再确定是否创建新数组应该可以吧
网吧 没工具,就不测试了
public static void main(String[] args) throws Exception {
String str = "1,2,1,3,2,4,5,3,2";
String[] arr = str.split(",");
String[] arr_temp = null;
Arrays.sort(arr);
String new_str = Arrays.toString(arr);
String[] str_sort = new String[5];
for (int i = 0; i < 5; i++) {
if (new_str.indexOf((i+1) + "") != -1) {
str_sort[i] = new_str.substring(new_str.indexOf((i+1) + ""), new_str.lastIndexOf((i+1) + "")+1);
}
System.out.println (str_sort[i]); // 把str_sort[i]用","分割后就是想要的动态数组
}
}
String[] arr_temp = null;
这行多余,去掉
在这行代码执行完后
String[] str_sort = new String[5];
for (int i = 0; i < 5; i++)
这两行中的5就可以替换为
arr数组中的最后一个元素
即arr[arr.length - 1]抱歉楼主,我以为数组的范围是已知的
if (new_str.indexOf((i+1) + "") != -1)返回结果
1
null
null
null
5
import java.util.Scanner;/**
* 用来保存字符串及其出现的个数
* @author les
*
*/
class Information
{
String test;
int num;
}public class TestStringMax {
String test; //用来保存要输入的字符串
public TestStringMax(String test)
{
this.test = test;
}
//返回动态生成的N个数组
public String[][] splitString()
{
ArrayList<Information> al = new ArrayList<Information>();
//用于记录不同的字符串出现的次数
int count = 1;
String[] str_array = test.split(",");
//将字符串保存到列表中
ArrayList<String> list = new ArrayList<String>();
for(int i=0;i<str_array.length;i++)
{
if(!str_array[i].equals("")) //相当与用来过滤多个逗号
list.add(str_array[i]);
}
while(list.size()!=0)
{
for(int i=1;i<list.size();i++)
{
if(list.get(0).equals(list.get(i)))
{
count++;
list.remove(i);
i--;
}
}
Information ifm = new Information();
ifm.test = list.get(0);
ifm.num = count;
al.add(ifm);
count = 1;
list.remove(0);
}
String[][] resultStr = new String[al.size()][];
for(int i=0;i<al.size();i++)
{
resultStr[i] = new String[al.get(i).num];
for(int j=0;j<al.get(i).num;j++)
{
resultStr[i][j] = al.get(i).test;
}
}
return resultStr;
}
public void display(String[][] str)
{
int i,j;
for(i=0;i<str.length;i++)
{
System.out.print("resultStr"+"["+i+"]=[");
for(j=0;j<str[i].length-1;j++)
{
System.out.print(str[i][j]+",");
}
System.out.println(str[i][j]+"]");
}
}
//主函数
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Please input the string:");
String test = sc.nextLine();
// System.out.println("String test=\""+test+"\"");
TestStringMax tsm = new TestStringMax(test);
String[][] resultStr = tsm.splitString();
tsm.display(resultStr);
}
}控制台:
输入:
Please input the string:hello,world,i,i,hello,world,i,i,love,love,i,i,i,hello,,world,,,world
显示:
resultStr[0]=[hello,hello,hello]
resultStr[1]=[world,world,world,world]
resultStr[2]=[i,i,i,i,i,i,i]
resultStr[3]=[love,love]
是啊。
就1和5两个字符串
其它为null,
不过这两个字符串还得split(","),
返回的不就是两个数组了吗
String str = "1,2,1,3,2,4,5,3,2,6,6,7,5,4,7,6,4,5,6,3,4,3";
String[] arr = str.split(",");
// String[] arr_temp = null;
Arrays.sort(arr);
String new_str = Arrays.toString(arr);
//
// System.out.println(arr[arr.length-1].toString());
String[] str_sort = new String[Integer.parseInt(arr[arr.length-1])];
for (int i = 0; i < str_sort.length; i++) {
if (new_str.indexOf((i+1) + "") != -1) {
str_sort[i] = new_str.substring(new_str.indexOf((i+1) + ""), new_str.lastIndexOf((i+1) + "")+1);
}
System.out.println (str_sort[i]); // 把str_sort[i]用","分割后就是想要的动态数组
}
}
HashMap<Integer, Collection<MtMmtContainers>> map = new HashMap<Integer, Collection<MtMmtContainers>>();
for (String value : values) {
MtMmtContainers item = (MtMmtContainers)mtmmtContainersDAO.findById(Integer.valueOf(value));
Integer warehouseId = item.getMdWarehouse().getId();
if (map.containsKey(warehouseId)) {
Collection<MtMmtContainers> col = map.get(warehouseId);
col.add(item);
} else {
Collection<MtMmtContainers> newCol = new ArrayList<MtMmtContainers>();
newCol.add(item);
map.put(warehouseId, newCol);
}
}
for(Map.Entry<Integer, Collection<MtMmtContainers>> entry:map.entrySet()) {
Collection<MtMmtContainers> col = entry.getValue();
this.destuff(entry.getValue(),entry.getKey());
}
String str = "1,2,1,3,2,4,5,3,2";
String[] arr = str.split(",");
Arrays.sort(arr);
String new_str = Arrays.toString(arr);
//HashSet 不允许要重复的数据
Set<String> s = new HashSet<String>();
for(String temp : arr) {
s.add(temp);
}
int strlen = s.size();//所以可以动态获得 你的数组 大小
String[] str_sort = new String[strlen];
for (int i = 0; i < strlen; i++) {
if (new_str.indexOf((i+1) + "") != -1) {
str_sort[i] = new_str.substring(new_str.indexOf((i+1) + ""), new_str.lastIndexOf((i+1) + "")+1);
}
System.out.println (str_sort[i]);
}
}
public static List<String[]> do3(){
String str = "1,2,3,4,5,6,7,8,9,4,5,8,5,6,6,6,1";
List<String[]> listStr = new ArrayList<String[]>();
Map<String, Integer> m = new HashMap<String, Integer>();
if(str != null && str != "") {
String[] a = str.split(",");
for(String tmp : a) {
if(m.containsKey(tmp)) {
m.put(tmp, m.get(tmp) + 1);
}else {
m.put(tmp, 1);
}
}
}
if(m.size() > 0) {
for(Entry<String, Integer> e : m.entrySet()) {
String[] s = new String[e.getValue()];
Arrays.fill(s, e.getKey());
listStr.add(s);
}
}
if(listStr != null && !listStr.isEmpty()) {
for(String[] s : listStr) {
for(int i=0;i<s.length;i++) {
System.out.print(s[i] + ",");
}
System.out.println();
}
}
return listStr;
}
Arrays.fill(s, e.getKey());
public class Algorithm {
/*
* 思路:把想同的一组字符放在一个ArrayList tempList中,然后把所有的tempList放到一个数组中。
*/
public List splitString(String name){
String[] splitName=name.split(",");//首先把传入的字符串分割好
List tempList=new ArrayList();
int count=0;
//判断有几个不同的数字。
for(int i=0;i<splitName.length;i++){
String tempString=splitName[i];
if(!tempList.contains(tempString)){//list 中不包含该数字,次数加1。
count=count+1;
tempList.add(tempString);//把该数字存放到list中。
}
}
List list=new ArrayList();
for(int j=0;j<count;j++){//循环比较
String tempString=(String)tempList.get(j);//tempList中存放的字符都是不同的,唯一的。
List temList=new ArrayList();
for(int k=0;k<splitName.length;k++){//循环判断。
String tempS=splitName[k];
if(tempS.equals(tempString)){//tempS==tempString往temList中存入该字符。
temList.add(tempS);
}
}
list.add(temList);
}
return list;
}
public static void main(String[] args){
Algorithm algorithm=new Algorithm();
String name="1,2,3,1,2,3,1,2,3,4";
List list=new ArrayList();
//调用方法
list=algorithm.splitString(name);
//循环取数。
for(int i=0;i<list.size();i++){
ArrayList al=(ArrayList)list.get(i);
String tempString="";
for(int j=0;j<al.size();j++){
tempString+=(String)al.get(j);
}
System.out.println("String is:"+tempString);
}
}}
import java.util.regex.*;public class ArrayGenerator {
public static void main(String[] args) {
//[1,1],[2,2],[3,3,3]
String str = "1,2,1,3,2,4,5,3,2";
String[] sArr = str.split(",");
Set<String> set=new HashSet<String>();
for(String s : sArr) {
set.add(s);
}
List<String> list = new ArrayList<String>(set);
Collections.sort(list);
for(String ss : list) {
System.out.println(Arrays.toString(generator(str, ss)));
}
} private static String[] generator(String str, String regex) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
List<String> list = new ArrayList<String>();
int count = 0;
while(m.find()) {
list.add(m.group());
count++;
}
String[] a = new String[count];
return list.toArray(a);
}
}
String[] testArr = test.split(",");
Arrays.sort(testArr);
for(int i=0;i<testArr.length-1;i++){
if(testArr[i].equals(testArr[i+1])){
System.out.print(testArr[i]);
}else{
System.out.println(testArr[i]);
}
}
System.out.println(testArr[testArr.length-1]);