[英]find longest linked lists in an array of linked lists
我有一個鏈表列表(鄰接表),鏈表的大多數長度為4,但有些長度會隨機地超過該長度。 我的目標是遍歷數組,找到長度大於4的那些(那是最簡單的部分),然后將索引添加到數組中,例如
for (int i = 0; i < 1000; i++){
if(sWorld[i].length > 4)
//add i to an array
// then sort the array
無法真正弄清楚如何做到這一點。 我試圖添加到一個墨跡列表,然后將鏈表添加到Array(),但是后來搞砸了。 我只是真的不知道如何在我的sWorld數組中添加'i'點,以說新數組im中的第一個位置將用於大於4號的那些。
任何幫助將非常感激!
編輯以澄清一點
我需要大小> 4的位置的索引,但是然后我想知道我得到的那些索引中具有最大大小的索引。 也許我在操作中不是100%清楚的,但是基本上我試圖在數組中的1000個索引中找到哪個具有最大的連接(最長的鏈表)才有意義?
我想知道數組的前10個連接索引(又名其中10個鏈表的大小最大)
您可以使用ArrayList
來存儲索引:
List<Integer> indexes = new ArrayList<Integer>();
for (int i = 0; i < 1000; i++){
if (sWorld[i].length > 4) {
//add i to a list (not an array yet)
indexes.add(i);
}
...
}
// then sort the list
// not necessary, as indexes are inserted in the right order, but if you must...
// Collections.sort(indexes);
// and, if you need an array instead of a list
Integer[] indexesArray = indexes.toArray(new Integer[indexes.size()]);
List
或ArrayList
用作可變長度數組。 雖然不如實際數組有效。
如上所示,以后無需對數組進行排序,但是,如果必須的話,可以使用Collections.sort()
。
另外,如果必須具有int[]
而不是Integer[]
,請檢查: 如何在Java中將List <Integer>轉換為int []?
更新:
您想知道更大數組的大小和索引,這是一個全新的問題。 下面是處理它的工作代碼。
基本上,每次找到大小大於4的數組時,都會在列表中添加一個對(index, size)
。 然后,此列表按大小降序排列。
在main()
方法的末尾,創建一個數組( int[] topTenIndexes
),其中包含10個最大數組的索引(索引按其數組長度的降序顯示)。 如果沒有足夠大的數組(長度> 4),則結果為-1。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Example {
public static void main(String[] args) {
List<ArrayIndexAndSize> indexes = new ArrayList<ArrayIndexAndSize>();
int[][] sWorld = {{1},{2,5,5,5,5},{3,6,6,6,6,6}};
for (int i = 0; i < sWorld.length; i++){
if (sWorld[i].length > 4) {
// add a pair (index, size) to the list
indexes.add(new ArrayIndexAndSize(i, sWorld[i].length));
}
//...
}
// then sort the list by array SIZE, in descending order
Collections.sort(indexes);
// Print it!
System.out.println(indexes);
/* output:
"[[Array index: 2; Array size: 6], [Array index: 1; Array size: 5]]"
*/
// Generating an array with the top ten indexes
int[] topTenIndexes = new int[10];
Arrays.fill(topTenIndexes, -1);
for (int i = 0; i < indexes.size() && i < 10; i++) {
topTenIndexes[i] = indexes.get(i).index;
}
// Print it
System.out.println(Arrays.toString(topTenIndexes));
/* output: [2, 1, -1, -1, -1, -1, -1, -1, -1, -1] */
}
public static class ArrayIndexAndSize implements Comparable<ArrayIndexAndSize> {
public int index;
public int size;
public ArrayIndexAndSize(int index, int size) {
this.index = index;
this.size = size;
}
/* Order by size, DESC */
/* This is called by Collections.sort and defines the order of two elements */
public int compareTo(ArrayIndexAndSize another) {
int thisVal = this.size;
int anotherVal = another.size;
return -(thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}
@Override
public String toString() {
return "[Array index: "+index+"; Array size: "+size+"]";
}
}
}
如果您有一個LinkedList<?>[] sWorld
(填寫您在?
使用的類型),請執行以下操作:
ArrayList<Integer> listOfLists = new ArrayList<>();
for (int i=0; i<sWorld.size(); i++) {
if (sWorld[i].size > 4) {
listOfLists.add(i);
}
}
Comparator<Integer> sizeComparator = new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return Integer.compare(sWorld[a].size(), sWorld[b].size());
}
}
Collections.sort(listOfLists, sizeComparator);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.