Scott Hollas
Scott Hollas

Reputation: 57

Call a method, renaming the variables as new one

I'm working on a small project, I want to make code a lot shorted, and easier, rather than writing it out multiple times. I have a method shown below, I want to use this method multiple times - but every String that is stored in the Text Field has to be kept - and not replaced. Eg.

Method:

public static void text(){
    TextField tf1 = new TextField();
}
text();

Upvotes: 1

Views: 80

Answers (3)

KP_JavaDev
KP_JavaDev

Reputation: 222

An ArrayList of String type probably help your code.

ArrayList TextField =new ArrayList();

TextField.add("item1");
TextField.add("item2");

Upvotes: 0

FatherMathew
FatherMathew

Reputation: 990

If I understood the question correct, I would do in the below way.

TextField tf[] = new TextField[100];
int clickCount = 0;

public static void text(){

tf[clickCount] = new TextField();
clickCount++;

}

You can initialize tf[] with the number you want. If you are not aware of the number, You can go for an ArrayList

Upvotes: 0

Adarsh Bhat
Adarsh Bhat

Reputation: 159

You should probably use a list of String type. A list would enable you to hold infinite data of the same type while keeping it under the same name since a list dynamically grows in size. you can then use an iterator or a for-each loop to access the data stored in the list.

The general syntax would be: List<String> textFieldList=new ArrayList<String>();

P.S. Remember to use the List of java.util package.

Upvotes: 3

Related Questions