简体   繁体   English

Java:传入一个字符串并将其存储

[英]Java: pass in a string and store it

How would I create a method that has the input of a string and output of all the strings that were input into it like a super string? 我将如何创建一个方法,该方法具有一个字符串的输入和输入到其中的所有字符串的输出,例如超级字符串?

for example in the main class: 例如在主类中:

a= "fido"
b= "rufus"
c= "dog"

superString(a); 
superString(b); 
superString(c); 

System.out.println(superString());  should be "fidorufusdog"

so far I have= 到目前为止,我有=

public static String superString (String sb) {
    StringBuilder ssb = new StringBuilder(32);
    ssb = ssb.append(sb);
    return ssb.toString();
}

My code below is what I am working on for a stock simulator: 我下面的代码是我正在使用的股票模拟器:

public class Operators {
  public static void operate(double price, double low, String company){
    double percent = (price/low-1)*100;
    double rpercent = Math.round(percent * 100.0) / 100.0; 
    StringBuilder sb = new StringBuilder(32);

    if(rpercent <= 10) {
      sb.append(company + " is trading at:");
      sb.append("the current price is:  " + price);
      sb.append("the 52 week low is:  " + low);
      sb.append("percent of 52 week low is:  " + rpercent);
    }
  }
}

The operate method is called in a for loop in my main method 506 times and I would like to take all 506 sb string and create a super string of all the results 在我的主方法中,在for循环中调用了操作方法506次,我想获取所有506 sb字符串并创建所有结果的超字符串

Declare that string builder as the static member containing class and initialize it only once 将该字符串生成器声明为包含类的静态成员,并仅对其初始化一次

static StringBuilder ssb;
public static String superString (String sb) {
    if(ssb == null)
          ssb = new StringBuilder(32);
    ssb = ssb.append(sb);
    return ssb.toString();
}

I hope I do not underestimate the depth of the question or have your question wrong, but to me it sounds like you are asking for the static keyword? 希望我不要小看问题的深度或您的问题有误,但对我来说,这听起来像是您要使用static关键字吗?

class SomeClass {

    static StringBuilder accumulator = new StringBuilder();

    public String superString (String sb) {
        SomeClass.accumulator.append(sb);
        return ssb.toString();
    }

}

This is simple usecase of the Java static keyword. 这是Java static关键字的简单用例。 Since accumulator is declared static there will be a single instance of the variable. 由于累加器被声明为静态,因此将只有一个变量实例。 And this single instance will be accessed by instances of the class SomeClass. 并且该单个实例将由SomeClass类的实例访问。 For example: 例如:

SomeClass a;
SomeClass b;

a.superString("aaa");
b.superString("bbb");

// now accumulator.toString() returns "aaabbb"

For this kind of probelm you've two choice : 对于这种探针,您有两种选择:

  1. Simple : create a static variable in the Java class, and manipulate it. 简单:在Java类中创建一个静态变量,然后对其进行操作。
  2. improve : create a design model which support your need. 改善:创建支持您需求的设计模型。

Ex 1 : 例1:

public class Operators {

    private static String text ="";

    public static String superString(String sb) {        
        if (sb != null) {
             text = text.concat(sb);
        }
        return text;
    }

}

Ex 2 : you can use a Collecion or a List of strings. 示例2:您可以使用Collecion或字符串列表

This is poor OOP design. 这是糟糕的OOP设计。 You would be much better off creating a class for Stock objects and overriding toString in the Stock class(or creating some other simple output method). Stock对象创建一个类并覆盖Stock类中的toString (或创建其他一些简单的输出方法)会更好。 Then add each instance of Stock to an array and call the each object's toString (or other output method you defined). 然后将Stock每个实例添加到数组中,并调用每个对象的toString (或您定义的其他输出方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM