Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

StringBuffer's into Array

Posted on 2000-03-12
6
Medium Priority
?
1,471 Views
Last Modified: 2008-02-26
I want to put 5 different Stringbuffer's into an Array that i can use the Index 0 to 4.
Can you help me?
0
Comment
Question by:MartinSchuster
6 Comments
 
LVL 3

Expert Comment

by:rjackman
ID: 2610039
instead of using array use vector


StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
StringBuffer sb3 = new StringBuffer();
StringBuffer sb4 = new StringBuffer();

Vector v = new Vector();
StringBuffer StrBuff[];
sb1.append(some data);
sb2.append(some data);
sb3.append(some data);
sb4.append(some data);

v.addElement(sb1);
v.addElement(sb2);
v.addElement(sb3);
v.addElement(sb4);

so while retrieving u have to do

for(int i = 0 ;i<v.size();i++)
{
   
  StrBuf[i] =(StringBuffer) v.elementAt(i);
   
}

RJackman
0
 

Accepted Solution

by:
darrenc earned 800 total points
ID: 2610981
Hi Martin,

The easiest way to do this is to simply initialise a StringBuffer array and then populate it.

Here's an example

*** test.java ***
import java.util.*;

public class test {
   public static void main(String args[]) {
        StringBuffer buffArray[] = new StringBuffer[5];
        buffArray[0] = new StringBuffer();
        buffArray[1] = new StringBuffer();
        buffArray[0].append("Hello");
        buffArray[1].append("Hi");
        System.out.println(buffArray[0].toString());
        System.out.println(buffArray[1].toString());
   }
}

*** end test.java ***

Hope that helps
Darren
0
 
LVL 1

Expert Comment

by:techtime
ID: 2611032
And what is the problem with that?
Though rjackman is right here is the code that seem to work & be exactly what you've asked for:

StringBuffer sbA[] = new StringBuffer[5];

for(int i=0;i<5;i++)
 sbA[i] = new StringBuffer();

for(int i=0;i<5;i++)
 sbA[i].append("Here is sb#"+i);

for(int i=0;i<5;i++)
  System.out.println(sbA[i]);

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:darrenc
ID: 2614048
Hi,

Yes you are right ... the original code fragment from rjackman would work (sorry if anyone thought I meant that the original comment was wrong)

Darren
0
 
LVL 1

Expert Comment

by:techtime
ID: 2614794
Well dear darrenc my comment had nothing to do with yours in fact I've posted it before you... Alas, the network says its word.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2751166
Martin, this looks like a duplicate of question http://www1.experts-exchange.com/Computers/Programming/Languages/Java/Q_10309649.html.

Did any of the comments on either question help you?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question