Let say I have 2 arrays
Array1 = 1,2,3,4,5
Array2 = a,b,c,d,e
String[] array = getResources().getStringArray(R.array.Array1);
That's work fine. But I don't want to use the code above again with another line
String[] array = getResources().getStringArray(R.array.Array2);
How do I get the below lines to work if
I had declared xxx as variable for array name
String xxx = Array1;
String[] array = getResources().getStringArray(R.array.xxx);
You can:
int xxx = R.array.Array1; //change to integer
String[] array = getResources().getStringArray(xxx); //pass the whole id
Instead of passing just the name of the resource, pass the whole ID (as integer).
If you still want to pass the name as string, you can:
String xxx = "Array1";
int resourceId = getResources().getIdentifier(xxx, "array", getPackageName());
String[] array = getResources().getStringArray(resourceId);
reference of the second option: https://stackoverflow.com/a/3476447/9038584
A simple util method in your activity or service:
String[] getArray(int id) {
return getResources().getStringArray(id);
}
Usage:
String[] array1 = getArray(R.array.array1);
Related
How can i put a list of strings, type string
in
String countryList[] = {a};
a is the variable that have the list
so, it will be
String countryList[] = {"heyyesGabon","heyyesGibraltar","heyyesGuinea"}; //and the others
this list comes from console.log in javascript
it always save the last item
From List to String array
String [] countryArray = countryList.toArray(new String[0]);
From String array to List
List<String> list = Arrays.asList(array);
You can use in-build function list.toArray(stringArray); it will return String[].
String countryList[] = new String[list.size()];
countryList = list.toArray(countryList);
Hope this will help you.
i have two String arrays mylist and listprice.
<string-array name="mylist">
<item>Bottle</item>
<item>Watch</item>
<item>Books</item>
<item>Mobile</item>
<item>Purse</item>
<item>Pen</item>
<item>Glass</item>
<item>Class</item>
<item>Rubber</item>
<item>Fan</item>
</string-array>
<string-array name="listprice">
<item>160</item>
<item>2600</item>
<item>200</item>
<item>26000</item>
<item>260</item>
<item>10</item>
<item>500</item>
<item>3000</item>
<item>3</item>
<item>380</item>
</string-array>
i want to initilize both the arrays items such that "bottle" equals "160","watch" equals "2600" and so on... how can i achieve it...thanks in advance...
You can use hash map for both array relation:
HashMap<String,String> map=new HashMap<String,String>();
for (int i = 0; i < getResources().getStringArray(R.array.mylist).length; i++) {
map.put(getResources().getStringArray(R.array.mylist)[i],getResources().getStringArray(R.array.listprice)[i]);
}
String[] itemList = getResources().getStringArray(R.array.mylist);
String[] itemPrice = getResources().getStringArray(R.array.listprice);
Now you can put all these in hashmap.
HashMap<String, String> items = new HashMap<>();
for(int i = 0; i<itemList.length; i++){
items.put(itemList[i], itemPrice[i]);
}
Now if you want to get price of Fan then just write
String price = items.get("Fan"); //it will be 380
OR you can make use of str.equals(str2) method for checking the item and then from the index getting the price from itemPrice array.
You can access string arrays with below code:
Resources res = getResources();
String[] mylist = res.getStringArray(R.array.mylist);
String[] listprice = res.getStringArray(R.array.listprice);
and you can use mylist[index] equals listprice[index]
Take two strings and concatinate the values .
private String[] concat(String[] A, String[] B) {
int aLen = A.length;
int bLen = B.length;
String[] C= new String[aLen+bLen];
System.arraycopy(A, 0, C, 0, aLen);
System.arraycopy(B, 0, C, aLen, bLen);
return C;
}
In java you can do something like with
String myList[]=getResources().getStringArray(R.array.mylist);
String myListPrice[]=getResources().getStringArray(R.array.listprice);
You can create a class lets say
public class ItemPrice{
private String listItemName;
private String listPrice;
public String getListItemName(){return listItemName;}
public String getListPrice(){return listPrice;}
public void setListItemName(String itemName){this.listItemName = itemName;}
public void setListPrice(String listPrice){this.listPrice = listPrice;}
}
Your array logic can be something like this
List<ItemPrice> itemPriceList = new ArrayList<ItemPrice>();
for(int i=0;i<myList.length;i++){
ItemPrice items = new ItemPrice();
items.setListItemName(myList[i]);
items.setListPrice(myListPrice[i]);
itemPriceList.add(items);
}
So you can get the itemPriceList which will contain both item along with its price encapsulated in one object. I am assuming both array length will be same and are mapped according to their index.
Do share if this is what you are looking for and if you require further help?
You can get your array like this:
String[] listprice = getResources().getStringArray(R.array.listprice);
String[] mylist = getResources().getStringArray(R.array.mylist);
I need to edit the string value in variable.
So,
00343755932
should be converted to:
0,0,3,4,3,7,5,5,9,3,2
because I must define each number as an variable array for readable one by one.
if I'm right you are trying to create an array from string. Use following code
String val = "00343755932";
int[] numberArray = new int[val.length()];
Matcher match = Pattern.compile("[0-9]").matcher(val);
int i = 0;
while(match.find()) {
System.out.println(match.group());
numberArray[i] = Integer.parseInt(match.group());
i++;
}
I am trying to add the data of textview into array. myLogClass is a array list and The codes is as follows:
String txt_datetime = txt_date.getText().toString();;
String txt_messageData = txt_message.getText().toString();
String txt_day = this.dayName;
List< myLogClass > results = null;
results = new ArrayList< myLogClass >();
results.add( new myLogClass( txt_datetime, txt_messageData, txt_day ) );
I have setter and getter method in myLogClass and has constructor with 3 variable which is as follows:
public diaryLogs(int dateTime, String messagetxt, String dayN){
setDayCode(dateTime);
setDateTime(messagetxt);
setDairyText(dayN);
}//end constructor.
While i tried to add method it says the constructor is undefined.
Thanks for your help in advance
To be able to add a diaryLogs instance to the result array, the ArrayList should be of diaryLogs. Here I made the modification before going to the other issue.
The definition of the diaryLogs constructor is (int dateTime, String messagetxt, String dayN), that being said, that function is expecting an int, and then two strings as parameters.
To get this working you need to convert the string txt_datetime to int before passing it to the constructor.
String txt_datetime = txt_date.getText().toString();;
String txt_messageData = txt_message.getText().toString();
String txt_day = this.dayName;
// Convert to Integer
int txt_datetime_int = Integer.parseInt(txt_datetime);
// Define the ArrayList as diaryLogs
List< diaryLogs > results = null;
results = new ArrayList< diaryLogs >();
// Pass as Integer
results.add( new diaryLogs( txt_datetime_int , txt_messageData, txt_day )
In this line:
results.add( new diaryLogs( txt_datetime, txt_messageData, txt_day ) );
txt_datetime is a String while Integer is expected. This is why you get this message.
is it possible to store different values into a multidimensional array such as int's and String's?
String[][] mainArray= new String[2][2];
mainArray[0][0] = 1;
mainArray[0][1] = "Name1";
mainArray[1][0] = 2;
mainArray[1][1] = "Name2";
this obviously doesn't work because 1 and 2 are not String values
yes you can store
try this
String[][] mainArray= new String[2][2];
mainArray[0][0] = String.valueOf(1);
mainArray[0][1] = "Name1";
mainArray[1][0] = String.valueOf(2);
mainArray[1][1] = "Name2";
You can create an Object array, and save Integers, which is the boxing of the primitive int.
Object[][] arr = new Object[2][2];
arr[0][0] = "hello";
arr[0][1] = Integer.valueOf(1);
arr[1][0] = Integer.valueOf(2);
arr[1][1] = "world";
Here is the solution
Object[][] arr=new Object[anysize][]; and you can do like this
arr[0][0]=1;
arr[1][0]="hello";
But while accessing this array you should also do this using Object only.Else there may be ClassCastException.