0

How can I find an object in Vector that contains the biggest number of elements? For instance:

int[] val1 = {1,2,3};
int[] val2 = {1};
int[] val3 = {1,2};
Vector<Object> d = new Vector<Object>();  // update
c.add(val1);
c.add(val2);
c.add(val3);
int answ = findBiggest(c);

In this example, answ should be equal to 0, because val1 contains 3 numbers.

Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217

3 Answers3

1
  • First of all, Vector is an obsolete class that should not be used anymore. If you chose your learning material yourself, abandon it and find something more up-to-date. If that's homework given by a teacher, tell them so.
  • Your code won't compile because you cannot add an int[] to a Vector<Integer>
  • You can loop over the elements of a collection using the enhanced for loop:

    for(int[] element : collection){ /* Do something with element */ }

  • The number of elements of an array is found via the length field of the array.
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • why is vector obsolete? What do you suggest as a better collection to use? – JJ Liu Jan 02 '12 at 09:23
  • @JJ Liu: ArrayList (introduced over 10 years ago in Java 1.2) should be used instead. Vector is synchronized, which makes it slower and is nearly always useless, and its API is polluted by methods from before the collections framework whose functionality is duplicated by methods from the List interface. – Michael Borgwardt Jan 02 '12 at 09:33
  • Thanks. ArrayList, Array, List, which one of these would you suggest to use in a particular situation? – JJ Liu Jan 02 '12 at 09:55
  • @JJ Liu: that of course depends on the situation. Use arrays if you're dealing with large amounts of primitives (for performance reasons), use List in APIs (allowing different implementations to be used), use ArrayList as your default List implementation, use LinkedList if you often insert/delete values from the list. – Michael Borgwardt Jan 02 '12 at 10:06
1

First thing to note is that your Vector is not well typed (should be Vector<int[]>) Next here is a quick way

public int findBiggest(Vector<int[]> c) {
    int max = -1;
    int ok = -1;
    int index = 0;
    for (int[] ints : c) {
        if (max < ints.length) {
            max = ints.length;
            ok = index;
        }
        index ++;
    }
    return ok;
}

public void test() {
    int[] val1 = {1,2,3};
    int[] val2 = {1};
    int[] val3 = {1,2};
    Vector<int[]> c = new Vector<int[]>();
    c.add(val1);
    c.add(val2);
    c.add(val3);
    int answ = findBiggest(c);
}

And as michael-borgwardt said, don't use Vector for that reason : Vector Obsolete. Briefly, this is due to synchronization issue. So use the List abstraction with its common implementation ArrayList. You can also use Collection (since index doesn't seem important here) rather than List.

Community
  • 1
  • 1
Andy Petrella
  • 4,345
  • 26
  • 29
1

I have answered this by taking the liberty of changing the datatype to arraylist, which I believe is what you originally wanted..

I also changed the generic type to Integer[] too. Because I believe there is an overhead associated with autoboxing, which I guess is minimal but good practice? (Can anyone confirm this?)

public static void main(String[] args) {
    Integer[] val1 = {1,2,3};
    Integer[] val2 = {1,2,3,3,3};
    Integer[] val3 = {1,2};
    ArrayList<Integer[]> c = new ArrayList<Integer[]>(); 
    c.add(val1);
    c.add(val2);
    c.add(val3);
    int answ = findBiggest(c);
    System.out.println(answ);
}

public static int findBiggest(ArrayList<Integer[]> list){
    int biggestSize = 0
    int biggestPos = -1;
    int i = 0;
    for(Integer[] el : list){
        if(el.length >= biggestSize){
            biggestSize = el.length;
            biggestPos = i;
        }
        i++;
    }

    return biggestPos;
}
AlanFoster
  • 8,156
  • 5
  • 35
  • 52
  • Autoboxing overhead increases the memory used per integer by at least 3 times and requires heap allocation. Not a problem if you're dealing with a few hundred. Huge problem if it's millions, or the code that deals with them is performance critical and autoboxed ints cause constant cache misses. – Michael Borgwardt Jan 02 '12 at 09:22