• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array's & Vector

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guy's,
I was trying to run simple program simple array and vector program. It compiled but at run time it is giving me error at vector out of bound. I tried with giving dimension in vector too, but it didn't work it out. Help me out. Thanks in advance.
----------------
Here is the code
import java.util.*;
public class arraytest
{
public static void main( String[] srgs)
{
int count,i,j;
String h = new String("Hello");
String m = new String();
count = 10;
int num[] = new int[count];
Vector number = new Vector();

for ( i = 0; i < count; i++)
num[i] = i;
for ( j = 0; j < count; j++)
System.out.println(num[j]);
System.out.print("Display Vectore now");
for ( i = 0; i < count; i++)
{
number.set(i,h);
System.out.println(number.get(i));
}
}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,
to add elements/objects in a vector, try using the addElement() method....your program will work fine.
here's the modified code and its output:
code:
import java.util.*;
public class arraytest
{
public static void main( String[] srgs)
{
int count,i,j;
String h = new String("Hello");
count = 10;
int num[] = new int[count];
Vector number = new Vector();

for ( i = 0; i < count; i++)
num[i] = i;
for ( j = 0; j < count; j++)
System.out.println(num[j]);
System.out.print("Display Vectore now");
for ( i = 0; i < count; i++)
{
number.addElement(h);
System.out.println(number.get(i));
}
}
}
output:
0
1
2
3
4
5
6
7
8
9
Display Vectore nowHello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
the set() method replaces the element of the vector with a new element.
.... hope this helps!
~Kavitha
 
Anil Kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for updating me
 
reply
    Bookmark Topic Watch Topic
  • New Topic