This question already has answers here:
Get variable by name from a String
(6 answers)
Closed last year.
Suppose I have a String Variable like this in Java:
String cat = "felix";
int felix = 6;
Is there some way in Java that I could compare the contents of cat - "felix" to the name of the int variable felix?
There is no way to do it with the method you are doing, but you can use hashmap:
Map<String,Integer> myMap = new HashMap<>();
myMap.put("felix", 6);
myMap.put("another key", -4);
int value = myMap.get("felix");
System.out.println(value);//prints 6
value = myMap.get("abc");//throws null pointer exception
Related
This question already has answers here:
Getting all names in an enum as a String[]
(26 answers)
Closed 3 years ago.
I'm having an Enum array. Now I want to convert it to a String array which contains the names of the enums returned by the method Enum#name(). Here's what I tried so far (The enum is called "Column".):
String[] stringArray = Arrays.asList(Column.values()).toArray(String[]::new);
I'm alway getting an ArrayStoreException. What can I do?
You need to stream the enum in order to first map the enum to String before creating the array:
String[] arrStr = Arrays.stream(FooEnum.values()) // create stream of enum values
.map(e -> e.toString()) // convert enum stream to String stream
.toArray(String[]::new); // convert stream to an array
This question already has answers here:
What happens when an object is assigned to another object
(4 answers)
Why are two empty ArrayLists with different generic types equal?
(4 answers)
Closed 4 years ago.
I have the below function to check if it is in order
public boolean order(List<String> value) {
List<String> tmp = value;
Collections.sort(tmp);
return tmp.equals(value);
}
Test:
assertTrue(route.order(Arrays.asList("a", "s", "d")));
assertFalse(route.order(Arrays.asList("a", "k", "c")));
but fail at 2nd test, why is it not false?
Here in below line:
List<String> tmp = value;
You are just copying reference of value list argument in tmp list and hence you are sorting on tmp and indirectly value list which is one and the same.
To solve the problem change the assignment of tmp variable to:
List<String> tmp = new ArrayList<>(value);
public boolean order(List<String> value) {
List<String> tmp = new ArrayList<>(value);
Collections.sort(tmp);
return tmp.equals(value);
}
Your asserts are negative to each other.
Both arrays are sorted. Maybe you wanted ("A", "C", "B").
Also, as the fellows said before, your sort is on the original list, you have to copy it first and then to sort.
This question already has answers here:
Why does the foreach statement not change the element value?
(6 answers)
Java Modifying Elements in a foreach
(4 answers)
Enhanced for loop not working for assigning values to an array (Java) [duplicate]
(2 answers)
Java: Foreach loop not working as expected on int array? [duplicate]
(3 answers)
Initializing an array in Java using the 'advanced' for each loop [duplicate]
(5 answers)
Closed 4 years ago.
A Java class with the following main:
public static void main(String[] args){
final int n = 3;
String[] array = new String[n];
for (String string : array) {
string = "OK";
}
for (String string : array) {
System.out.println(string);
}
}
produces the output "null, null, null,". Eclipse IDE suggests me that "The value of the local variable ref is not used". Why?
N.B.: I know that I have to iterate the array using this code:
for(int i=0; i<n ;i++)
array[i] = "OK";
This question already has answers here:
Sort List in reverse in order
(9 answers)
Closed 5 years ago.
in my app Android I have a set of data I want to bind to a spinner.
But of these I would like a particular value to be seen as the first in the spinner list.
public String [] getDescriptionCategories () {
Set <String> categories = products.keySet ();
String [] result = new String [categorie.size ()];
int i = 0;
for (String cat: categories) {
result [i ++] = cat;
}
return result;
}
The result is ["Altro","Prodotti","Utenti"], but I wish it was ["Utenti", "Prodotti", "Altro"]
How do I set it up?
You should use return Collections.reverse(result);. It will reverse your list. If you want to sort the list by alphabetical order, see this answer. Hope it helps :)
Use Collection class ie sort(List) and reverse(List)
This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 6 years ago.
I have tried this
ScreenDumpParser dump = new ScreenDumpParser();
Map btn_bound = dump.parse();
Iterator iterator = btn_bound.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
List<Integer> value = btn_bound.get(key);
System.out.println(key);
}
but this line
List<Integer> value = btn_bound.get(key);
gives error:
Type mismatch: cannot convert from Object to List<Integer>
I need to print all the values along with the key in one single row.
If you want to add a value to your List you should use:
value.add( btn_bound.get(key));
and if the button is a list or something else you must add more then one value to your list with
value.addRange( btn_bound.get(key));
And if you want to get a value:
Object foo = value.get(btn_bound.get(key));