Getting repeating loop infinit - android

I am using nested for loop for getting from arraylist and that loop run infinte
private ArrayList prepareData()
{
ArrayList arrayList = new ArrayList<>();
for(int i=0;i<android_image_urls.length;i++){
Androidversionclass androidversion = new Androidversionclass();
androidversion.setAndroid_image_url(android_image_urls[i]);
arrayList.add(androidversion);
for (int k= 0; k < android_image_second.length; k++) {
Androidversionclass androidversionclass1 = new Androidversionclass();
androidversion.setAndroid_image_second(android_image_second[k]);
arrayList.add(androidversion);
}
for (int l = 0; l < android_image_thirds.length; l++) {
Androidversionclass androidversionclass2 = new Androidversionclass();
androidversion.setAndroid_image_thirds(android_image_thirds[l]);
arrayList.add(androidversion);
}
for (int m = 0; m < android_image_fourth.length; m++) {
Androidversionclass androidversionclass3 = new Androidversionclass();
androidversion.setAndroid_image_fourth(android_image_fourth[m]);
arrayList.add(androidversion);
}
}
return arrayList;
}

Related

display Multidimensional Arrays android

I'm using android studio and with opencv 3.4.0. I want to display a Multidimensional Array in text view. I have an a array "candidats_result" and I copied her values into a Multidimensional array " finale". after runing my code I got this result in emulator display. what I have to change in my code to get the text displayed
double [][] R_finale = new double[20][20];
int ZZ = 0;
int ZE = 0;
int EE = 0;
for(int i=0;i<19;i++){
for(int j=0;j<19;j++){
R_finale[i][j] = candidats_result[ZZ];
ZZ++;
}
}
String [][] finale = new String [20][20];
//showing the array in android
for(int i=0;i<19;i++) {
for (int j = 0; j < 19; j++) {
finale[i][j] = Double.toString(R_finale[i][j]);
}
}
String [][] details = new String[20][20];
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i<20;i++) {
for (int j = 0; j < 20; j++) {
details[i][j] = String.valueOf(R_finale [i][j]);
builder.append(details + ";");
}
}
textView.setText(String.valueOf(builder));
Notice below lines:
for ( int i = 0 ; i<20;i++) {
for (int j = 0; j < 20; j++) {
details[i][j] = String.valueOf(R_finale [i][j]);
builder.append(details + ";"); //should be builder.append(details[i][j] + ";");
}
}

Compare two array-list element and get un-common element

I have done something like this :
public class MainActivity extends AppCompatActivity {
ArrayList<String> al = new ArrayList<String>();
ArrayList<String> a2 = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
a2.add("C");
a2.add("C");
a2.add("E");
a2.add("B");
a2.add("D");
a2.add("F");
for (int i = 0; i < al.size(); i++) {
for (int j = 0; j < a2.size(); j++) {
if (al.get(i).equals(a2.get(j))) {
a2.remove(j);
Log.e("array 2 ", a2.toString());
break;
}
}
}
}
}
-- But my output is like this :
[C, E, B, D, F]
[C, B, D, F]
[C, D, F]
[C, F]
[C]
-- i am having same elements in arraylist 2, then also i am getting 'C', it should be null .i.e zero un-common value.
You are messing with the index when you remove the a2 items directly inside for loop. Refer to my solution below
ArrayList<String> al = new ArrayList<>();
ArrayList<String> a2 = new ArrayList<>();
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
a2.add("C");
a2.add("C");
a2.add("E");
a2.add("B");
a2.add("D");
a2.add("F");
ArrayList<String> tempToDelete = new ArrayList<>();
for (int i = 0; i < al.size(); i++) {
for (int j = 0; j < a2.size(); j++) {
if (al.get(i).equals(a2.get(j))) {
tempToDelete.add(a2.get(j));
break;
}
}
}
a2.removeAll(tempToDelete);
for shorter method, you can just do this:
a2.removeAll(al);
Try This it will help you
ArrayList<String> al = new ArrayList<String>();
ArrayList<String> a2 = new ArrayList<String>();
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
a2.add("C");
a2.add("C");
a2.add("E");
a2.add("B");
a2.add("D");
a2.add("F");
Integer a = null;
for (int i=0; i<a1.size(); i++)
{
a = a1.get(i);
if (a2.contains(a)
{
a2.remove(a);
a1.remove(a);
i--;
}
}
ArrayList<String> finaldata = new ArrayList<String>();
finaldata.addAll(a1);
finaldata.addAll(a2);
// finaldata = { A}
Just replace break; with this:
j = -1;
code:
for (int i = 0; i < al.size(); i++) {
for (int j = 0; j < a2.size(); j++) {
if (al.get(i).equals(a2.get(j))) {
a2.remove(j);
Log.e("array 2 ", a2.toString());
j = -1;
}
}
}
I recommend you to use Set's instead of ArrayList. You can find some info here.
Also, here you can read some examples of using java Set in set operations in this stackoverflow answer.

how to filter json array by some condition

how to filter values from json array i want to filter that if string zero1check =0 add only that json array in Category_name.add(object.getString("dish_name")); where "day":"m1"
if zero1check =0 add in Category_name.add(object.getString("dish_name")); only that json array which contain "day":"m2"
{
"status":1,
"data":
[
{
"school_name":"testing12",
"menu_title":"Menu1",
"dish_name":null,
"day":"m1"
}
,
{
"school_name":"testing12"
,"menu_title":"Menu1",
"dish_name":null,
"day":"m1"
}
,
{
"school_name":"testing12"
,"menu_title":"Menu1",
"dish_name":null,
"day":"m2"
}
,
{
"school_name":"testing12"
,"menu_title":"Menu1",
"dish_name":null,
"day":"m2"
}
]
}
static ArrayList<Long> Category_ID = new ArrayList<Long>();
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> menu_name = new ArrayList<String>();
String zero1check;
JSONObject json2 = new JSONObject(str);
status = json2.getString("status");
if (status.equals("1")) {
JSONArray school = json2.getJSONArray("data");
for (int i = 0; i < school.length(); i++) {
JSONObject object = school.getJSONObject(i);
Category_ID.add((long) i);
Category_name.add(object.getString("dish_name"));
menu_name.add(object.getString("menu_title"));
String[] mVal = new String[school.length()];
for (int k = 0; k < school.length(); k++) {
mVal[k] =
school.getJSONObject(k).getString("menu_title");
menu_nametxt.setText(mVal[0]);
}
Try this.. then change zero1check datatype as int
JSONArray school = json2.getJSONArray("data");
for (int i1 = 0; i1 < school.length(); i1++) {
JSONObject object = school.getJSONObject(i1);
if(zero1check == 0)
{
if(object.getString("id").equals("m1"))
{
Category_name.add(object.getString("dish_name"));
}
}
else if(zero1check == 1)
{
if(object.getString("id").equals("m2"))
{
Category_name.add(object.getString("dish_name"));
}
}
}

How to display high value from array calculation

I'm trying to build a project expert system with a Certainty factor method in android, but I'm having trouble when displaying the highest value inputted from the user.
here is my code:
public void diagn() {
for (int i = 0; i < listCheckBox.size(); i++) {
if (listCheckBox.get(i).isChecked()) {
idGejala.add(listGejala.get(i).getId_gejala());
}
}
for (int i = 0; i < idGejala.size(); i++) {
Cursor kursor = db.getNilaiCFdanIDpenyakit(idGejala.get(i));
while (!kursor.isAfterLast()) {
Kaidah kaidahku = new Kaidah();
kaidahku.setId_penyakit(kursor.getString(0));
kaidahku.setNilai_CF(kursor.getDouble(1));
kaidah.add(kaidahku);
kursor.moveToNext();
}
}
Penyakit penyakit;
for (int i = 0; i < listPenyakit.size(); i++) {
penyakit = listPenyakit.get(i);
CfPenyakit cfpenyakit = new CfPenyakit();
cfpenyakit.setId_penyakit(penyakit.getId_penyakit());
cfpenyakit.setNama_penyakit(penyakit.getNama_penyakit());
for (int j = 0; j < kaidah.size(); j++) {
Kaidah kaidahku = kaidah.elementAt(j);
if (penyakit.getId_penyakit().equals(kaidahku.getId_penyakit())) {
cfpenyakit.addCF(kaidahku.getNilai_CF());
}
}
if (cfpenyakit.getFinalCF() > 0)
cf.add(cfpenyakit);
}
String[] penyakitku = new String[cf.size()];
String[] cfku = new String[cf.size()];
String[] idPenyakit = new String[cf.size()];
for (int i = 0; i < cf.size(); i++) {
CfPenyakit data = cf.elementAt(i);
penyakitku[i] = data.getNama_penyakit();
idPenyakit[i] = data.getId_penyakit();
double orim = (data.getFinalCF()*100);
cfku[i] = Double.toString(data.getFinalCF() * 100);
cfku[i] = String.format("%.2f", orim) + "%";
}
db.close();
Intent intent = new Intent(this, Diag2.class);
intent.putExtra("penyakit", penyakitku);
intent.putExtra("cf", cfku);
intent.putExtra("idPenyakit", idPenyakit);
startActivity(intent);
finish();
}

I want to delete a null column from a 2d array in android?

i have problem in data binding in expandable list view. here i use
ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
ExpandListGroup for data binding. but in sum 2d array there is null value.Data is coming dynamically .
eg:
String [] [] array1 = [[one,two,three,null],[seven,six,null]] ;
I want to remove the null column from this two dimensional array
You need to take a intermediate arraylist and result array and follows the below steps.
First copy the source array(containing null) into arraylist.
Then remove null from arraylist
Then create new array and copy data from arraylist to array.
private void remove_nulls()
{
String [] [] array1 = {{"one","two","three",null},{"seven","six",null}} ;
ArrayList<ArrayList<String>> contact = new ArrayList<ArrayList<String>>();
for(int i=0;i<array1.length;i++)
{
ArrayList<String> con = new ArrayList<String>();
for(int j=0;j<array1[i].length;j++)
{
if(array1[i][j]!=null)
con.add(array1[i][j]);
}
if(con.size()>0)
contact.add(con);
}
String [] [] array2 = new String[array1.length][];
for(int i=0;i<contact.size();i++)
{
array2[i]=new String[contact.get(i).size()];
for(int j=0;j<contact.get(i).size();j++)
{
array2[i][j]=contact.get(i).get(j);
}
}
}
Unless there is some trick to the question...
String[][] array1 = {{"one", "two", "three", null}, {"seven", "six", null}};
List<String[]> newList = new ArrayList<>();
for (int i = 0; i < array1.length; ++i) {
List<String> currentLine = new ArrayList<>();
for (int j = 0; j < array1[i].length; ++j) {
if (array1[i][j] != null) {
currentLine.add(array1[i][j]);
}
}
//create the array in place
newList.add(currentLine.toArray(new String[currentLine.size()]));
}
//no need to use an intermediate array
String[][] array2 = newList.toArray(new String [newList.size()][]);
//And a test for array2
for (int i = 0; i < array2.length; ++i) {
for (int j = 0; j < array2[i].length; ++j) {
System.out.print(array2[i][j] + " ");
}
System.out.println();
}
System.out.println("Compared to...");
//Compared to the original array1
for (int i = 0; i < array1.length; ++i) {
for (int j = 0; j < array1[i].length; ++j) {
System.out.print(array1[i][j] + " ");
}
System.out.println();
}

Categories

Resources