[英]Storing String on array java
我想知道是否可以在String數組上存儲String變量? 我不能很好地解釋它,但這是我做的:
String st1 = "",st2 = "",st3 = "",st4 = "";
String[] str = {st1,st2,st3,st4};
不幸的是,當我使用for循環時,str獲取st1和st2以及st3和st4的值而不是它自己的變量..
這就是我想要完全按照我的想法做的事情。每當有一個String數組時,例如:
String[] containsValue = { "hi", "hello", "there" };
String strHi, strHello, strThere;
String[] getContainsValue = { strHi, strHello, strThere };
for (int x = 0; x < getContainsValue.length; x++) {
getContainsValue[x] = containsValue[x];
}
The value of:
strHi = "hi"
strHello = "hello"
strThere = "there";
基本上我想將containsValue []的值傳遞給3個String,它是存儲在getContainsValue []中的strHi,strHello,strThere。 然后使用for循環來對它們賦值來自containsValue []。 這是真的嗎? 如果是這樣,那么你可以給我一些格式怎么做? 謝謝..
您可以使用Map<K,V>
。
Map<String,String> map=new HashMap<String,String>();
map.put("strHi","hi");
map.put("strHello","hello");
map.put("strThere","there");
System.out.println(map.get("strHello"));
您可以使用枚舉類作為所需的數組:
public enum EnumModifE {
str1("1"), str2("2"), str3("3");
String value;
EnumModifE(final String s) {
this.value = s;
}
public void setValue(final String s) {
this.value = s;
}
}
public class EnumModifM {
public static void main(final String[] args) {
for (final EnumModifE eme : EnumModifE.values()) {
System.out.println(eme + "\t" + eme.value);
}
EnumModifE.str1.setValue("Hello");
EnumModifE.str2.setValue("all");
EnumModifE.str3.setValue("[wo]men");
for (final EnumModifE eme : EnumModifE.values()) {
System.out.println(eme + "\t" + eme.value);
}
}
}
產量
str1 1
str2 2
str3 3
str1 Hello
str2 all
str3 [wo]men
請參閱有效Java使用枚舉
您正在尋找的概念是“l值”。 簡而言之,當您使用變量時,您是使用變量中包含的值,還是在談論變量本身,以便您可以在其中存儲其他內容? 您希望調用getContainsValue
數組具有strHi
, strHello
和strThere
l值。 不幸的是,在Java中無法做到這一點。 使用strHi
, strHello
和strThere
初始化getContainsValue
strThere
使用這些變量的值 ,而不是它們的l值 。
讓我們退后一步,更多地討論l值與值(有時是r值)。 請考慮以下代碼段:
int i = 17;
i = i + 1;
第二行顯然不是一個等式; 這將是荒謬的。 相反,它是一項任務 。 i在作業左右兩側的含義是不同的。 在右側, 我的意思是使用該變量的值,在這種情況下為17.在左側, i表示變量本身,作為存儲值的目的地。 即使它們看起來相同,但在右側使用i是為了它的值(更具體地說,它的r值 ),並且在左側使用i是為了它的l值 。
在Java中,沒有辦法在數組初始值設定項中表示變量的l值,因此您嘗試執行的操作不起作用。 正如其他人所指出的那樣,在其他語言中,如使用&(address-of)運算符,這是可能的。
由於Java具有有限的表達l值的方式,因此通常通過對對象的引用來表達“存儲某物的地方”的概念。 然后,可以使用此引用存儲到該對象的字段中或調用該對象上的方法 。
假設我們有一個這樣的類:
class MyContainer {
String str;
void setString(String s) { str = s; }
String getString() { return str; }
}
然后我們可以重寫您的代碼以執行以下操作:
String[] containsValue = { "hi", "hello", "there" };
MyContainer hiCont = new MyContainer();
MyContainer helloCont = new MyContainer();
MyContainer thereCont = new MyContainer();
MyContainer[] getContainsValue = { hiCont, helloCont, thereCont };
for (int x = 0; x < getContainsValue.length; x++) {
getContainsValue[x].setString(containsValue[x]);
}
那么你可以用它。
Map<String,String> map = new HashMap<String,String>();
String[] str = {"hi","hello","there"};
for(int x = 0; x < str.lenght;x++){
map.put(str[x],"something you want to store");
}
最好的事情可能是使用Map和store作為鍵值對。
Map<String,String> myKVMap=new HashMap<String,String>();
myKVMap.put("strHi","value1");
myKVMap.put("strHello","value2");
myKVMap.put("strThere","value3");
這樣您就可以消除所有變量名稱和值問題。
我認為你應該像Map
一樣使用Collection
。
Map用於以Key-Value
Pairs的形式存儲數據。
在您的情況下,假設Key是String,Value也是String。
Map<String, String> mp = new Map<String, String>();
mp.put("str1","Hi");
mp.put("str2","Hello");
You can iterate over it like the below.
for(Map.Entry<String, String> ar : mp.entrySet()){
System.out.println("Key: "+ar.getKey()+" :: "+"Value: "+ar.getValue());
}
使用地圖是個好主意。 另一種方法是實例化類變量,然后賦值將起作用。
public void testTransfer() {
String containsValue[] = { "hi", "hello", "there" };
Data strHi = new Data();
Data strHello = new Data();
Data strThere = new Data();
Data[] getContainsValue = { strHi, strHello, strThere };
for (int x = 0; x < getContainsValue.length; x++) {
getContainsValue[x].value = containsValue[x];
}
// print out
System.out.println(strHi.value);
System.out.println(strHello.value);
System.out.println(strThere.value);
}
class Data {
private String value;
}
如果我理解你的問題,你希望能夠通過先在數組中查找然后進行賦值來分配常規的String變量。
我同意其他響應者的意見,如果你發現這種方法是必要的,那可能是不明智的。 但是本着純粹的問答精神,這就是方式:
interface StringAssigner {
void assign( String strValue );
}
// ...
String strHi, strHello, strThere;
StringAssigner[] asaGetContainsValue = {
new StringAssigner() { @Override public void assign( String strValue ) { strHi = strValue; } },
new StringAssigner() { @Override public void assign( String strValue ) { strHello = strValue; } },
new StringAssigner() { @Override public void assign( String strValue ) { strThere = strValue; } }
};
// ...
for (int x = 0; x < asaGetContainsValue.length; x++) {
asaGetContainsValue[x].assign( containsValue[x] );
}
拒絕吧。
我同意這里的其他答案,這感覺就像是一種解決方法,但不知道那是什么東西我不能建議更好的東西。
但是要回答這個問題:但是,您可以將字符串包裝在簡單類中,並將該類的對象引用存儲在數組和strHi
, strHello
和strThere
。 這種方式即使您更改類中的字符串屬性,類對象本身也不會更改,因此您將看到您要查找的行為。
或者,您可以像其他人建議的那樣使用HashMap。 在您的情況下,如果您仍想使用getContainsValue
數組,則可以存儲鍵:
Map<String,String> map = new HashMap<String,String>();
map.put("strHi","");
map.put("strHello","");
map.put("strThere","");
String[] containsValue = { "hi", "hello", "there" };
String[] getContainsValue = { "strHi", "strHello", "strThere" };
for (int x = 0; x < getContainsValue.length; x++) {
map.put(getContainsValue[x], containsValue[x]);
}
然后, map.get("strHi")
將按預期返回"hi"
。
沒有簡單的方法可以用Java做你想做的事情。 你需要的是等效的C / C ++地址 - 運算符( &
)...或者Perl能夠使用字符串作為變量名。 Java中都不支持這些。
理論上,如果變量在實例變量中,則可以使用反射來訪問和更新它們。 但是這樣做的代碼是混亂,低效和脆弱的。 它不適用於局部變量。
你最好尋找一個不同的問題解決方案; 例如,使用Map,正如其他答案所建議的那樣。
或者只是滿足於使用一個開關或一系列if else if
測試和原始變量的一些笨重(但強大且合理有效)的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.