如何将一个数组拆分成多个固定长度的数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将一个数组拆分成多个固定长度的数组相关的知识,希望对你有一定的参考价值。
参考技术A 背景:最近开发了一个需求:需要将一个数组拆分成多个等长度的数组,如,将 ['百','变','小','樱','魔','术','卡','哦'] 拆分成 ['百','变','小','樱'] 和 ['魔','术','卡','哦'] 。
首先需要用到 javascript 的 slice() 方法。
slice() 方法可从已有的数组中返回选定的元素: arrayObject.slice(start,end) 。
例:
['百','变','小','樱','魔','术','卡','哦'].slice(2,4) ,返回 : ["小", "樱"]
那么,我们需要做的事情就是做循环,依次按顺序和规则取出原数组中的元素即可,例:将['百','变','小','樱','魔','术','卡','哦']拆分为长度为2的4个数组。
const ArrayList = [] let index = 0 while (index < 8) ArrayList.push(['百','变','小','樱','魔','术','卡','哦'].slice(index, index += 2))
最终可以得到ArrayList中的元素为 [百','变'], ['小','樱'], [魔','术'], ['卡','哦']
Java:将按字符串长度排序的字符串数组按字符串长度拆分为多个数组
【中文标题】Java:将按字符串长度排序的字符串数组按字符串长度拆分为多个数组【英文标题】:Java: Split array of strings sorted by string length into several arrays by string length 【发布时间】:2020-08-14 01:56:16 【问题描述】:我目前有一个按字符串长度排序的字符串数组,例如:
String[] array = [a,b,c,ab,cd,abc,abcde,fghij,klmno]
如何根据字符串大小将此数组转换为多个数组,同时跟踪每个数组的字符串大小?我想要的是:
String[] array1 = [a,b,c]
String[] array2 = [ab,cd]
String[] array3 = [abc]
String[] array5 = [abcde,fghij,klmno]
我可能正在考虑为此使用矩阵,但不知道要这样做。
【问题讨论】:
【参考方案1】:使用System:arraycopy
仅使用数组的解决方案:
import java.util.Arrays;
public class Main
public static void main(String[] args)
String[][] arraysList = new String[1][];
String[] array = "a", "b", "c", "ab", "cd", "abc", "abcde", "fghij", "klmno" ;
int srcPos, row = 0;
for (int i = 0; i < array.length; i++)
srcPos = i;
while (i < array.length - 1 && array[i].length() == array[i + 1].length())
i++;
// Create a new array to store the current set of strings of equal length
String[] subarray = new String[i - srcPos + 1];
// Copy the current set of strings of equal length from array to subarray[]
System.arraycopy(array, srcPos, subarray, 0, subarray.length);
// Assign subarray[] to arraysList[][]
arraysList[row++] = subarray;
// Copy arraysList[][] to temp [][], increase size of arraysList[][] and restore
// arrays from temp [][] to arraysList[][]
String[][] temp = arraysList;
arraysList = new String[row + 1][subarray.length];
for (int j = 0; j < temp.length; j++)
arraysList[j] = temp[j];
// Drop the last row which was created to store a new subarray but there was no
// more subarrays to store and therefore it is empty.
arraysList = Arrays.copyOf(arraysList, arraysList.length - 1);
// Display the subarrays
for (String[] arr : arraysList)
System.out.println(Arrays.toString(arr));
输出:
[a, b, c]
[ab, cd]
[abc]
[abcde, fghij, klmno]
使用List
和数组的解决方案:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main
public static void main(String[] args)
List<String[]> list = new ArrayList<String[]>();
String[] array = "a", "b", "c", "ab", "cd", "abc", "abcde", "fghij", "klmno" ;
int srcPos;
for (int i = 0; i < array.length; i++)
srcPos = i;
while (i < array.length - 1 && array[i].length() == array[i + 1].length())
i++;
String[] subarray = new String[i - srcPos + 1];
System.arraycopy(array, srcPos, subarray, 0, subarray.length);
list.add(subarray);
// Display the subarrays
for (String[] arr : list)
System.out.println(Arrays.toString(arr));
输出:
[a, b, c]
[ab, cd]
[abc]
[abcde, fghij, klmno]
【讨论】:
【参考方案2】:您可以使用Map
将字符串长度关联到该长度的字符串子数组:
String[] array = "a", "b", "c", "ab", "cd", "abc", "abcde", "fghij", "klmno";
Map<Integer, String[]> map = new HashMap<>();
for(int j=0, i=1; i<=array.length; i++)
if(i == array.length || array[i].length() > array[j].length())
map.put(array[j].length(), Arrays.copyOfRange(array, j, i)) ;
j = i;
for(Integer len: map.keySet())
System.out.format("%d : %s%n", len, Arrays.toString(map.get(len)));
输出:
1 : [a, b, c]
2 : [ab, cd]
3 : [abc]
5 : [abcde, fghij, klmno]
【讨论】:
【参考方案3】:我的解决方案与@QuickSilver 的相同,只是不太清楚。 既然我在这里,我也放我的,因为我有专门的时间,但我再说一遍,我建议跟随他。
代码
public static void main(String[] args)
String[] array = "a", "b", "c", "ab", "cd", "abc", "abcde", "fghij", "klmdfwetdfgdfgdfgdg";
HashMap<Integer, List<String>> hashMap = new HashMap<>();
int strLength = array[0].length();
for (String s : array)
while (true)
if (s.length() == strLength)
if (hashMap.get(strLength) != null)
List<String> temp = hashMap.get(strLength);
temp.add(s);
hashMap.put(strLength, temp);
else
List<String> strings = new LinkedList<>();
strings.add(s);
hashMap.put(strLength, strings);
break;
else
strLength = s.length();
System.out.println(hashMap);
【讨论】:
【参考方案4】:为了快速访问,您还可以使用列表列表。
String[] array = new String[]"a","b","c","ab","cd","abc","abcde","fghij","klmno";
List<List<String>> lists = new LinkedList<>();
// you will have to update this number based on the maximum length of string you are expecting
for (int i = 0; i < 6; i++)
lists.add(new LinkedList<>());
for (String a: array)
lists.get(a.length()).add(a);
System.out.println(lists);
这里,第一个列表用于大小,内部列表用于实际字符串。
注意:这仅适用于较小的字符串。如果您有长度为 1、2、100 的字符串。您可能应该使用 HashMaps,因为这种方法会浪费大量内存。
使用 Java8:
String[] array = new String[]"a","b","c","ab","cd","abc","abcde","fghij","klmno";
List<List<String>> lists = IntStream.range(0, 6).<List<String>>mapToObj(
i -> new LinkedList<>()).collect(Collectors.toCollection(LinkedList::new));
Arrays.stream(array).forEach(a -> lists.get(a.length()).add(a));
System.out.println(lists);
【讨论】:
【参考方案5】:最好创建一个Map<Integer, List<String>>
,其中键是字符串的长度,值是类似大小的字符串列表。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SimpleArray
public static void main(String[] args)
String[] array = new String[]"a","b","c","ab","cd","abc","abcde","fghij","klmno";
Map<Integer, List<String>> map = new HashMap<>();
for (int i = 0; i < array.length; i++)
List< String> temp = map.getOrDefault(array[i].length(),new ArrayList<>());
temp.add(array[i]);
map.put(array[i].length(),temp);
System.out.println(map);
【讨论】:
以上是关于如何将一个数组拆分成多个固定长度的数组的主要内容,如果未能解决你的问题,请参考以下文章