将java中的数组拆分为较小数组的最佳方法是什么?
发布时间:2020-12-15 04:23:42 所属栏目:Java 来源:网络整理
导读:将 java方法中的数组拆分为较小数组的最佳方法是什么?我希望能够将任何大小的数组放入takeReceipts(String []) //Can handle any size array public void takeReceipts(String[] receipts){//split array into smaller arrays,and then call handleReceipts
将
java方法中的数组拆分为较小数组的最佳方法是什么?我希望能够将任何大小的数组放入takeReceipts(String [])
//Can handle any size array public void takeReceipts(String[] receipts){ //split array into smaller arrays,and then call handleReceipts(String[]) for every smaller array } //This method can only handle arrays with the size of 5 or less private void handleReceipts(String[] receipts){ myNetworkRequest(receipts); } 编辑: 因此,将数组复制到另一个数组似乎效率不高.会这样的吗? public void takeReceipts(String[] receipts){ int limit = 5; int numOfSmallerArrays = (receipts.length/limit)+(receipts.length%limit); int from = 0; int to = 4; for (int i = 0; i < numOfSmallerArrays; i++){ List<String> subList = Arrays.asList(receipts).subList(from,to); from =+ limit; to =+ limit; } } 解决方法
你可以使用
Arrays.copyOfRange() :
int from = 0; int to = 4; String[] subArray = Arrays.copyOfRange(receipts,from,to) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |