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.
Related
I have 3 string list and I want to add all values to menus but it gives error which is "Invalid index 0, size is 0". Briefly, menus is null and how can I add them?
private List<List<Restaurant.Menu>> menus = new ArrayList<>();
ArrayList<String> MenuName = new ArrayList<>();
ArrayList<String> FoodName = new ArrayList<>();
ArrayList<String> FoodPrice = new ArrayList<>();
//I get values in DB. DB is full.
MenusName = tinydb.getListString("MenuName");
FoodName = tinydb.getListString("FoodName");
FoodPrice = tinydb.getListString("FoodPrice");
int restaurantCounter = 0;
int menuCounter = 0;
for (int j = 0; j < MenusName.size(); j++)
{
menus.get(restaurantCounter).get(j).name = MenusName.get(j))
}
Example, I created them for each value, it works but if string is long, it enforces app and I wait 10 sec. for this process. I need efficient way. Thanks in advance.
menus.get(resCounter).add(new Restaurant.Menu());
menus.get(resCounter).get(menuCounter).foods.add(new Restaurant.Food());
.
.
menus.get(resCounter).get(menuCounter).name = MenuName.get(i));
menus.get(resCounter).get(menuCounter).foods.get(foodCounter).name = FoodName.get(i);
menus.get(resCounter).get(menuCounter).foods.get(foodCounter).price = FoodPrice.get(i);
You seem to be confusing arrays with lists here. Arrays have a fixed size once initialised, Lists don't. You need to add something to your menus list using menus.add(/*Add Menu Object here*/);
You haven't add any values to menus.so the object doesn't contain value at the index 0. This might be a reason.
I couldn't find a solution so I changed structure. I get all strings in restaurants and I used gson to convert them before store them. Also you can use gson for each string. It it same logic.
Gson gson = new Gson();
ArrayList<String> gsonString = new ArrayList<>();
for(int i=0; i<restaurants.size(); i++)
gsonString.add(gson.toJson(restaurants.get(i)));
tinydb.putListString("tinyRestaurant",gsonString);
Convert again...
Gson gson = new Gson();
for(int i=0; i<tinydb.getListString("tinyRestaurant").size(); i++)
restaurants.add(gson.fromJson(tinydb.getListString("tinyRestaurant").get(i), Restaurant.class));
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);
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 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 there a way to sort a multidimensional Object?
Object[][] datetable= new Object[2][2];
datetable[0][0] = 2;
datetable[0][1] = "Name2";
datetable[1][0] = 1;
datetable[1][1] = "Name1";
If not, how would I sort a multidimensional int array?
int[][] numtable= new int[2][2];
numtable[0][0] = 2;
numtable[0][1] = 20;
numtable[1][0] = 1;
numtable[1][1] = 10;
I would like to sort it using the first set of numbers (numbtable[x] or datetable[x])
I can get a multidimensional string to sort no problem, but these I have trouble with.
Sorting arrays of int's is done through using Integer objects and using a comparator, similar to hotveryspicy's answer, as here:
Integer[][] numtable = new Integer[2][2];
numtable[0][0] = 2;
numtable[0][1] = 20;
numtable[1][0] = 1;
numtable[1][1] = 10;
Arrays.sort(numtable, new Comparator<Integer[]>() {
#Override
public int compare(Integer[] int1, Integer[] int2) {
return int1[0].compareTo(int2[0]);
}
});
However, from your comments, you seem to indicate you'll have a 5x12 array of objects interspersed with strings and ints which don't relate to each other (the sorting loses any connection between your items, which is what makes me think this), which you seem to be merging into one multidimensional array instead of separate lists (which is how i'd probably go).
if you do convert to a List of ints/strings, then use the following to sort them, in this case, i show the int version:
List<Integer> myList = new ArrayList<Integer>();
myList.add(2);
myList.add(1);
Collections.sort(myList);
System.out.println("myList is: " + myList);