How to add Dynamic String value to String array in java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string name called label,which is dynamically having values.I want to add this every time generated values into array list.
List<String> container = new ArrayList<String>(Arrays.asList(label));
I tried this way but its printing only the last value, where I'm doing wrong, any suggestion?

In this every time the dynamically changing value of label will be added into the container.
List<String> container = new ArrayList<String>();
String label = "Sample1";
container.add(label);
label = "Sample2";
container.add(label);
label = "Sample3";
container.add(label);
System.out.println("container : "+container);
But you are creating new list every time. using the label variable
List<String> container = new ArrayList<String>(Arrays.asList(label));

I think everytime you are resetting your container variable by the declaration:
List<String> container = new ArrayList<String>(Arrays.asList(label));
Ideally you should do it only at first time, after that simply add the newly created label:
container.add(label);

Related

Array Constants can only be used in initializers Error [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am new to Java Programming.I want to fetch data from the database and show it in JTable.
This is my current code.
String columns[]={"Transaction_id","User_name","Amount","Recharge_Spending","Spend_by"};
Object data[][]=new Object[200][5];
int i=0;
while(rs.next())
{
result_Transaction_id[i]=rs.getInt("f.Transaction_id");
result_User_name[i]=rs.getString("U.User_name");
data[i][5]={(Object)result_Transaction_id[i],(Object)result_User_name[i]};//This is where i get error
++i;
}
JTable View_table =new JTable(data,columns);
I want to collect the results of the query and add them in the specified object.
But i get the following error "Array constants can only be used in initializers".
I found a lot of solutions to "Array Constants can only be used in initializers" error, but those don't suit my case.
An "array constant" means { ... }.
As the error is trying to tell you, you can only use that syntax when declaring an array variable.
To assign an array to an existing expression, use new TypeName[] { ... }.
The error is the way you declare your array.
As stated here: https://stackoverflow.com/a/10520659/3558900 You can only declare an array NOT like this:
String columns[]={"Transaction_id","User_name","Amount","Recharge_Spending","Spend_by"};
BUT
String[] s;
s = new String[] {"Transaction_id","User_name","Amount","Recharge_Spending","Spend_by"};

Why Random random = new Random()? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi guys rather new to java, I am just having a bit of problem understanding some of the basics.
When you declare a random value you use Random random = new Random(), can someone please explain what the first Random, second random and "new Random()" does and the logic behind it?
Just a note, the first commenter is correct, you probably could have found this through a bit of Googling, but here's the answer as best as I can explain it:
Let's take that code:
Random random = new Random();
The first random just says what type of data the variable is going to store - in this case, "Random." The second random is the name of the variable. You can call this almost anything you want, "random," "ran," even something completely unrelated like "ThisIsAVar." Then you set the new variable (using the "=") to a new Random type.
A more generic example would be (as "Random" in the last explanation messes with the grammar):
string NewString = new String("Hello there!");
You're creating a new variable called NewString with type of string, and setting that to a new String type with the parameter "Hello there!"

How to remove Object in Java List? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have one existing code, where the written line is
private Rule[] ruleList;
Where Rule is a CLASS.
I want to remove all Rules which are added here in ruleList.
but as I click right it shows like this
If it is a List, how can I delete these rules from ruleList?
Thanks
The ruleList is an array and not a List. Thus you will not find a clear method.
But you can use
Arrays.fill(ruleList, null);
You have an array and not a list. Solution :
ruleList = new Rule[ruleList.length];
To remove all items from a List use clear()
If you want to remove all items in an array from a List use the remove method.
Example
public class Rules {
public static void main(String[] args) {
Rule[] rules = new Rule[2];
rules[0] = new Rules.Rule();
rules[1] = new Rules.Rule();
//Scenario 1
List<Rule> ruleList = new ArrayList<Rule>();
ruleList.add(Arrays.asList(rules)); //adds array to list
ruleList.clear(); //removes all items from List
//Scenario2
ruleList.add(Arrays.asList(rules)); //adds array to list
ruleList.remove(Arrays.asList(rules)); //remove all rules in [] form list
//Scenario3
Arrays.fill(rules, null); //removes all elements in array
}
static class Rule{
}
}
It is important to determine whether you are using a List or an Array. The provided code depicts an array, however the verbiage keeps referring to a List.
If you actually have a List and want to remove all elements see Scenario 1 in the example.
If you actually have a List and want to remove all elements in the array from the List see Scenario 2 in the example.
If you actually have array and want to remove all elements from the array see Scenario 3 in the example.

how to put values into HashMap<int,List<String>> [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to put and get the below values in/from HashMap as Key and values
Key Value
1 "a"
1 "b"
1 "C"
2 "x"
2 "y"
2 "z"
Well the general idea would be to put the String values in your List<String>. The add method is good for that.
Later you should put the key (1 or 2 according to your example) with the List<String> in your map. The put method is good for that.
It expects a key (your integer number) and a value (your list of strings).
try this..
HashMap<Integer, List<String>> map = new HashMap<>();
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
map.put(1, list);
If you need some direction here is a link with all you need to know about HashMap.
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

Need to get string part between two / in a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to java need help
the main string i have is
eg.
"String1/string2/string3/string4/all_free.sdx"
"file1/file2/string3/string4/all_free.sdx"
the end result i need is to be able to isolate and get string3
i can indexof but not able to achieve it in few steps need brainy people help as i am new to JAVA
If you know it's the third item that you want to get, one simple approach could be using split method, like this:
String myString = "String1/string2/string3/string4/all_free.sdx";
String string3 = myString.split("/")[2];
The call of split("/") produces an array of strings like this:
{"String1", "string2", "string3", "string4", "all_free.sdx"}
Now you can apply the subscript operator to grab the element that you want.

Categories