Home > database >  is there a way to have values added to a vector using obj.add(i,random.nextInt(MAX_RAND_NUM)); and t
is there a way to have values added to a vector using obj.add(i,random.nextInt(MAX_RAND_NUM)); and t

Time:12-18

I have the following code and for the Vector section, there is no number being added to the Vector array when using 'obj.add(i,random.nextInt(MAX_RAND_NUM));'. This is confirmed by the output of '[]' at the end. The problem seems to start with 'System.out.println("" i ": " obj.get(i));' not printing the value of 'obj.get(i)'.

Output:

FEC - Arrays
==> Using Java Array
0: 12
1: 24
2: 93
3: 52
4: 43
5: 2 <== smallest
6: 24
7: 19
8: 39
9: 64
Using java.util.ArrayList
Sorted Array:
[2, 12, 19, 24, 24, 39, 43, 52, 64, 93]
=================
==> Using java.util.Vector
Using java.util.Vector
Sorted Vector:
[]

code:

import java.util.Vector;
import java.util.ArrayList;
import java.util.Collections;


class Main {

  static final int ARRAY_SIZE = 10;
  static final int MAX_RAND_NUM = 100;

  public static void main(String[] args) {
    System.out.println("FEC - Arrays");

    System.out.println("==> Using Java Array");

    int[] nums = new int[ARRAY_SIZE];
    java.util.Random random = new java.util.Random();
    int smallest = MAX_RAND_NUM;
    for (int i = 0; i < nums.length; i  ) {
        nums[i] = random.nextInt(MAX_RAND_NUM);
        if (nums[i] < smallest) {
          smallest = nums[i];
        }
    }

    for (int i = 0; i < nums.length; i  ) {
      System.out.print(""   i   ": "   nums[i]);
      if (nums[i] == smallest) {
        System.out.print(" <== smallest");
      }
      System.out.println();
    }
    // Sort the Array 
    System.out.println("Using java.util.ArrayList");
    System.out.println("Sorted Array:");
    java.util.Arrays.sort(nums);
    System.out.println(java.util.Arrays.toString(nums));

    System.out.println("=================");

    System.out.println("==> Using java.util.Vector");
    // YOUR CODE HERE
    smallest = MAX_RAND_NUM;
    Vector<Integer> obj = new Vector<Integer>(ARRAY_SIZE);

   // obj.add(0,random.nextInt(MAX_RAND_NUM));
   // System.out.println(obj.get(0));

    for(int i = 0; i < obj.size(); i  )
    {
      obj.add(i,random.nextInt(MAX_RAND_NUM));
      if (obj.get(i) < smallest) 
      {
          smallest = obj.get(i);
        }
    }


    for (int i = 0; i < obj.size(); i  ) {
      
      System.out.println(""   i   ": "   obj.get(i));
      if (obj.get(i) == smallest) 
      {
        System.out.print(" <== smallest");
      }
      System.out.println();
    }
    // Sort the Vector
    System.out.println("Using java.util.Vector");
    System.out.println("Sorted Vector:");
    Collections.sort(obj);
    System.out.println(obj);

  }
}

CodePudding user response:

The body of the loop below never execute because obj.size is 0.

                          ERROR
                            |
                            V
    for(int i = 0; i < obj.size(); i  )
    {
      obj.add(i,random.nextInt(MAX_RAND_NUM));
      if (obj.get(i) < smallest) 
      {
          smallest = obj.get(i);
        }
    }

Instead, use ARRAY_SIZE.


    for(int i = 0; i < ARRAY_SIZE; i  )
    {
      obj.add(i,random.nextInt(MAX_RAND_NUM));
      if (obj.get(i) < smallest) 
      {
          smallest = obj.get(i);
        }
    }

CodePudding user response:

The size of the Vector is 0 after you initialize it since there are no elements; your loop condition should compare with ARRAY_SIZE instead of obj.size().

for(int i = 0; i < ARRAY_SIZE; i  ){
    // add elements
}
  • Related