简体   繁体   中英

find longest linked lists in an array of linked lists

I have an array of linked lists (an adjacency list) most of the lengths of the linked lists are 4 but there are some that will randomly have more than that. My goal is to go through the array and find the ones that are more than length 4 (thats the easy part) then add the index's to an array so something like

for (int i = 0; i < 1000; i++){
    if(sWorld[i].length > 4)
         //add i to an array
         // then sort the array

couldnt really figure out how to do this. i tried to add to a inked list then linked list toArray() but then it was messing up. i just dont really know how to add the 'i' point in my sWorld array to say the first position in the new array im going to use for the ones that are grater than size 4.

Any help would be much appreciated!

EDITED TO CLARIFY A BIT

i need the indexes of the the locations that are size > 4, but then i want to know which of those indexes that i get have the greatest size. Maybe i wasnt 100% clear in my op, but basically im trying to find which of the 1000 indexes in the array have the most connections (the longest linked list) make sense?

I want to know the top 10 connected indexes of the array (aka which 10 have the greatest size of linked list)

You can use an ArrayList to store the indexes:

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()]);

A List , or an ArrayList , work as variable-length arrays. Though not as efficiently as an actual array.

As seen above, there is no need to sort the array later, but, if you must, you can use Collections.sort() .

Also, if you must have an int[] instead of an Integer[] , please check: How to convert List<Integer> to int[] in Java?

Update:

As you want to know the size and index of the bigger arrays, it's a whole new problem. Below is a working code that deals with it.

Basically, everytime you find an array with size larger than 4, you add a pair (index, size) to the list. This list is then ordered by size, in descending order.

At the end of the main() method, an array is created ( int[] topTenIndexes ) which contains the indexes of the 10 biggest arrays (the indexes are presented in descending order of it's array's length). The result is -1 when there weren't enough big (length > 4) arrays.

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+"]"; 
        }
    }

}

If you have an array LinkedList<?>[] sWorld (fill the type you're using in for ? ), then do this:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM