Results 1 to 2 of 2
  1. #1
    nuggetman4 is offline Member
    Join Date
    May 2010
    Rep Power

    Default Advanced Looping

    How do i print the elements to the command line using a for loop without hard coding any numbers in the for loop?

    Java Code:
    Vector names = new Vector();

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Ljubljana, Slovenia
    Rep Power


    Vectors still have indexes, as a vector is nothing more than an array wrapped in a class, that can grow or shrink it as needed, so the following:
    Java Code:
    for(int i = 0; i < names.size(); i++) {
    Is perfectly valid. Next, do you know about generics? The way you defined your vector means it will return Object instances, and you have to guess what type the returned information is, and cast it.
    Java Code:
    Vector<String> names = new Vector<String>();
    This way, the Vector can only hold Strings, and even better, the get method returns a String instead of an Object.
    Also, using the enhanced for loop would be simpler in this case:
    Java Code:
    for(String s: names)
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. JAI: Java Advanced Imaging Install problem
    By panagath in forum New To Java
    Replies: 2
    Last Post: 02-13-2011, 01:29 AM
  2. Replies: 0
    Last Post: 05-04-2010, 11:11 AM
  3. Java Advanced Imaging -> RGB values
    By PHANTOMIAS in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2010, 01:43 PM
  4. Java arrays (not sure if this question is advanced)
    By aga010607 in forum Advanced Java
    Replies: 1
    Last Post: 11-24-2009, 01:43 PM
  5. Arrays Problem (Advanced Java...Need Help)
    By Zebra in forum New To Java
    Replies: 9
    Last Post: 05-02-2008, 01:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts