I am making an app in which there are list of questions and respective answers.
Questions are in one string array, while answers are in another string array.
I have implemented the following in a wish to shuffle the questions. (Of course the answers need to be linked to that question, else meaningless)
Code:
selected_Q = new String[totalnoofQ];
selected_A = new String[totalnoofQ];
int[] random_code = new int[totalnoofQ];
for (int i = 0; i < totalnoofQ; i++)
{
random_code[i] = i;
}
Collections.shuffle(Arrays.asList(random_code));
for (int j = 0; j < totalnoofQ; j++)
{
int k = random_code[j];
selected_Q [j] = databank_Q [k];
selected_A[j] = databank_A [k];
}
The code reports no fatal error, but the selected_Q is still in sequential order. Why?
Could you please show me how can I amend the codes? Thanks!!!
You shuffle a list created using random_code, but random_code is not modified.
You need to create a temporary list based on random_code. Shuffle this list and then use it to fill the selected_X arrays.
Something like this should work :
int[] random_code = new int[totalnoofQ];
for (int i = 0 ; i < totalnoofQ ; i++) {
random_code[i] = i;
}
List<Integer> random_code_list = new ArrayList<Integer>(); // Create an arraylist (arraylist is used here because it has indexes)
for (int idx = 0 ; idx < random_code.length ; idx++) {
random_code_list.add(random_code[idx]); // Fill it
}
Collections.shuffle(random_code_list); // Shuffle it
for (int j = 0 ; j < totalnoofQ ; j++) {
int k = random_code_list.get(j); // Get the value
selected_Q[j] = databank_Q[k];
selected_A[j] = databank_A[k];
}
Related
I have data from JSON like
5,7,9
and i want to save in arrayList as
"","","","","",5,"",7,"",9
what should i do?
try to change code like below in main forloop
Change your weeklyDataList like below
ArrayList<int[]>weeklyDataList = new ArrayList<int[]>();
Code in Forloop
int[] numbers = new int[characters.length()];
for (int j = 0; j < characters.length(); j++) {
numbers[j] = Integer.parseInt(characters.getString(j));
}
weeklyDataList.add(i,numbers);
I have 50 Buttons coded like this:
button_list.set(0,(Button) findViewById(R.id.button0));
button_list.set(1,(Button) findViewById(R.id.button1));
button_list.set(2,(Button) findViewById(R.id.button2));
button_list.set(3,(Button) findViewById(R.id.button3));
button_list.set(4,(Button) findViewById(R.id.button4));
button_list.set(5,(Button) findViewById(R.id.button5));
button_list.set(6,(Button) findViewById(R.id.button6));
button_list.set(7,(Button) findViewById(R.id.button7));
button_list.set(8,(Button) findViewById(R.id.button8));
button_list.set(9,(Button) findViewById(R.id.button9));
button_list.set(10,(Button) findViewById(R.id.button10));
button_list.set(11,(Button) findViewById(R.id.button11));
button_list.set(12,(Button) findViewById(R.id.button12));
button_list.set(13,(Button) findViewById(R.id.button13));
button_list.set(14,(Button) findViewById(R.id.button14));
button_list.set(15,(Button) findViewById(R.id.button15));
.
.
.
How can I put this all in a loop?
When I run the below code I get NullPointerExceptions, I guess meaning the Buttons are not recognized when I attempt to use findViewById. Does anyone know what is wrong with the following code and how I can fix it?
Button[] bttn = new Button[50];
String ids[] = new String[50];
for(int i=0; i<50; i++)
{
ids[i] = "button" + Integer.toString(i);
}
for(int i=0; i<50; i++)
{
int resID = getResources().getIdentifier(ids[i], "id", "your.package.name");
bttn[i] = (Button) findViewById(resID);
}
for(int i=0; i<50; i++)
{
button_list.set(i, bttn[i]);
}
You can do it in a single loop
Button[] buttons = new Button[50];
for(int i = 0; i < buttons.length; i++){
button[i] = (Button) findViewById(getResources().getIdentifier("button" + i, "id", getPackageName());
buttonList.set(i, button[i]); // if your list is already built
//buttonList.add(button[1]); // if you are building your list
}
I use getPackageName() as I assume you are in an Activity, if that is not the case you need to get a reference to the context and use context.getPackageName(); This goes the same for the getResources() call as it also needs a reference to your context.
Hey every one i want to get data from JSON but i could not get.. because the service come array inside array so anyone have Idea to solve, please
"services":[{"main_category_id":"78","main_category_name":"Repairing",
"product": [{"product_id":"3","product_name":"Washing Machine",
i have code like but only product data comes but service data exception
JSONArray contentServices = dataContent
.getJSONArray("services");
JSONObject Service_items = contentServices.getJSONObject(0);
List<CompanyServicesBean> jsonServicesList = new ArrayList<CompanyServicesBean>();
for (int j = 0; j < Service_items.length(); j++) {
JSONObject services = items.getJSONObject(j);
CompanyServicesBean serviceBean = new CompanyServicesBean();
serviceBean.setmain_category_id(services
.getString("main_category_id"));
ServiceBean.setmain_category_name(services
.getString("main_category_name"));
serviceBean.setproduct_id(services
.getString("product_id"));
serviceBean.setproduct_name(services
.getString("product_name"));
serviceBean.setduration_type(services .getString("duration_type"));
serviceBean.setduration_min(services
.getString("duration_min"));
}
You have a services array of Json Object
Inside each Json Object theres is another array product
So After getting services
for (int j = 0; j < Service_items.length(); j++) {
JSONObject services = items.getJSONObject(j);
CompanyServicesBean serviceBean = new CompanyServicesBean();
}
You need to get product array from serviceBean Object like this
for (int j = 0; j < services.getJsonArray("product").length(); j++) {
JSONObject product = items.getJSONObject(j);
}
In my app I have array size 400 elements. My task is Those elements are send to webservice for inserting.But it is not supported.So split the array into pieces and send to webservice.How is it
Something like this, perhaps?
String[] stringArray = new String[400];
//lets assume that the array has objects in it.
int index = 0;
for(int i = 0; i < stringArray.length; i++) {
String[] temp = new String[20];
for(int x = 0; x < 20) {
if(stringArray[index] != null) temp[x] = stringArray[index];
index++;
}
//The temp array is filled with objects from the other array, so send it to the webservice.
sendArrayToWebService(temp);
}
I'm tring to do a simple matrix initialization in android and I get Error: java.lang.ArrayIndexOutOfBoundsException. I'm trying this:
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id] = 0;
}
where id is a value between 1 and 5,sirid.length is a number that reflects number of images from different categorys. For example,I want for category 1 to have something like this :
vot[0][1]=0;
vot[1][1]=0;
vot[2][1]=0;
...etc
Where is my mistache?
try this
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id-1] = 0;
}
array index start from 0
you set the size of the vot array to sirid.lengh by id but the array start index value from 0 to (size)(size value not included) see the for loop
I think because you initialize the array on id.
After that you call to vot[i][id] which then will alway be 1 too high.
f.e.
if you create new int[3][3]
you can only call positions 0,1 and 2
good luck
That is because id has been considered as Length of the String and you r trying to access the same element... but the last element is vat[i][id-1] but u r trying to get vat[i][id]
So better use this..
for (int i = 0; i < sirid.length; i++) {
for(int j = 0; j<id ; j++){
vot[i][j] = 0;
}
}