Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Append a string to Vector element

  1. #1
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Append a string to Vector element

    Hi again, I don't find any method that append my string to the vector element.
    	Vector<String> v = new Vector<String>();
     
    	...
     
    	for(int i=0; i<v.size() ;i++)
    		v.elementAt(i).concat(" hello");
    This just return the v.elementAt(i) with " hello" at the end. but doesn't modify the element. So i should asignate the returned string to other variable .

    I can't do this asignation: v.elementAt(i) = v.elementAt(i).concat(" hello");

    Imagine all v.elements has 1234 already. Now I want to concat this (I want this output):
    v.elementAt(0) = "1234 hello"
    v.elementAt(1) = "1234 hello"
    v.elementAt(2) = "1234 hello"

    but I only get
    v.elementAt(0) = "1234"
    or
    v.elementAt(0) = " hello"
    If I use this i get replaced 1234 for hello, instead append it
    	for(int i=0; i<v.size() ;i++)
    		v.add(i, "hello");


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Append a string to Vector element

    The Vector class has other methods that may be useful to get and set its contents.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Rexshine (February 3rd, 2013)

  4. #3
    Member
    Join Date
    Jul 2012
    Location
    Spain
    Posts
    42
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Append a string to Vector element

    Hehe I'm lucky I know a bit about setter and getter , otherwise I could not understand you .
    Didn't expect that I could do this.

    Thank you-

    SOLVED:
    for(int i=0; i<v.size() ;i++){
    		 	v.set(i, v.get(i)+" hello");
    			System.out.println(v.elementAt(i));
    		}

Similar Threads

  1. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  2. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM
  3. problem searching for a String element in a parallel array
    By david185000 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2010, 01:24 PM
  4. String Vector
    By nasi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 3rd, 2010, 11:45 PM
  5. Add white spices before the String element
    By bookface in forum Java Theory & Questions
    Replies: 1
    Last Post: March 23rd, 2010, 08:50 PM