Solved

StringBuffer's into Array

Posted on 2000-03-12
6
1,451 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
Comment Utility
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 400 total points
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Expert Comment

by:darrenc
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java continue statement 10 69
pairs challenge 5 44
for i loop in grovy 1 32
recursion example 16 66
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now