Create a arraylist from output of a for loop - android

I am trying to get a data set as [{a,b,c,d} {e,f,g,h}] but I'm getting it as [a,b,c,d,e,f,g,h] using below mentioned code. What should I do to achieve that. any help will be highly appreciated. I'm new to android so please be kind enough to answer me. thanks in advance
List<String> CartItemEntityList = new ArrayList<String>();
for (int i = 0; i < mCartList.size(); i++) {
if (!mCartList.isEmpty()) {
Product product = mCartList.get(i);
int quantity = product.quantity;
double subTotal = product.subTotal;
CartItemEntity = product.items;
for (int j = 0; j < CartItemEntity.size(); j++) {
Item item = CartItemEntity.get(j);
subMenuCode = item.subMenuCode;
mainMenuCode = item.mainMenuCode;
price = item.price;
CartItemEntityList.add(mainMenuCode);
CartItemEntityList.add(String.valueOf(price));
CartItemEntityList.add(String.valueOf(quantity));
CartItemEntityList.add(subMenuCode);
}
}
System.out.println("CartItemEntityList " + CartItemEntityList);
}

I was able to achieve this using json serialization and this is my answer
JSONObject jsonObj = new JSONObject();
jsonObj.put("MainMenuCode",
item.getMainMenuCode());
jsonObj.put("Price", item.getPrice());
jsonObj.put("Quantity", product.getQuantity());
jsonObj.put("SubMenuCode",
item.getSubMenuCode());
String a = jsonObj.toString();
CartIddtemEntityList.add(a);

you have to create array of arraylist.
Try something like below:
ArrayList<ArrayList<String>> group = new ArrayList<ArrayList<String>>(mCartList.size());
for (int i = 0; i < mCartList.size(); i++) {
List<String> CartItemEntityList = new ArrayList<String>();
if (!mCartList.isEmpty()) {
Product product = mCartList.get(i);
int quantity = product.quantity;
double subTotal = product.subTotal;
CartItemEntity = product.items;
for (int j = 0; j < CartItemEntity.size(); j++) {
Item item = CartItemEntity.get(j);
subMenuCode = item.subMenuCode;
mainMenuCode = item.mainMenuCode;
price = item.price;
CartItemEntityList.add(mainMenuCode);
CartItemEntityList.add(String.valueOf(price));
CartItemEntityList.add(String.valueOf(quantity));
CartItemEntityList.add(subMenuCode);
}
}
System.out.println("CartItemEntityList " + CartItemEntityList);
group.add(CartItemEntityList);
}

Related

For loop last item issue

I am using a for loop in another for loop, Problem is I want Left options, Right Options and answers, but only getting last item of each. how will I get that? Please help me out
for (int i = 0; i < jsonArray.length(); i++) {
DataModel model = new DataModel();
jsonObject = jsonArray.getJSONObject(i);
LeftOpts = jsonObject.getString("LQUESTION");
RightOpts = jsonObject.getString("RQUESTION");
Ans = jsonObject.getString("TRUE_ANS");
String[] leftItem = LeftOpts.split("#");
String[] rightItem = RightOpts.split("#");
String[] ansItem = Ans.split("#");
for (int j = 0; j < leftItem.length; j++) {
item1 = leftItem[j];
Log.v("Left", item1);
model.setLOpt1(item1);
item2 = rightItem[j];
model.setROpt1(item2);
item3 = ansItem[j];
model.setAns1(item3);
myList.add(model);
}
You could try this:
for (int j = 0; j < leftItem.length; j++) {
DataModel model = new DataModel(); //move to here
item1 = leftItem[j];
Log.v("Left", item1);
model.setLOpt1(item1);
item2 = rightItem[j];
model.setROpt1(item2);
item3 = ansItem[j];
model.setAns1(item3);
myList.add(model);
}

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] + ";");
}
}

Filter number from Array of string and get the index of last two greater number in android

I have a ArrayList that has value like [Value,Sum3,121,data8input,in:21::7,7.00,9.01] and I want to extract only number as the output should be like this [3,121,8,21,7,7.00,9.01] and then have to rearrange ascending and then get the index of last two number as result will be [21,121].
My tried code below,
for (int i = 0; i < arrayString.size(); i++) {
Pattern p = Pattern.compile("-?\\d+(,\\d+)*?\\.?\\d+?");
List<String> numbers = new ArrayList<String>();
Matcher m = p.matcher(arrayString.get(i).getvalue);
numbers.addAll(m);
for (int j = 0; j < numbers.size(); j++) {
Log.d("REMEMBERFILTER", allCollection.get(i).getTextValue());
}
}
}
do something like this, though it is not exactly memory efficient as I am using another list.
ArrayList<String> tempList = new ArrayList<>();
for (int i = 0; i < yourArrayList.size(); i++) {
tempList.add(yourArrayList.get(i).replaceAll("[^0-9]", ""));
}
//Arrange in ascending order
Collections.sort(tempList);
//Also try to remove those indexes which has only letters with
tempList.removeAll(Arrays.asList("", null));
for (int i = 0; i < tempList.size(); i++) {
Log.d("+++++++++", "" + tempList.get(i));
}
//You can get the last two or any element by get method of list by //list.size()-1 and list.size()-2 so on
This is a way to do it, finalArray has the 2 numbers you want:
String[] str = new String[] {"Value", "Sum3", "121", "data8input", "in:21::7", "7.00,9.01"};
StringBuilder longStringBuilder = new StringBuilder();
for (String s : str) {
longStringBuilder.append(s).append(" ");
}
String longString = longStringBuilder.toString();
String onlyNumbers = " " + longString.replaceAll("[^0-9.]", " ") + " ";
onlyNumbers = onlyNumbers.replaceAll(" \\. ", "").trim();
while (onlyNumbers.indexOf(" ") > 0) {
onlyNumbers = onlyNumbers.replaceAll(" ", " ");
}
String[] array = onlyNumbers.split(" ");
Double[] doubleArray = new Double[array.length];
for (int i = 0; i < array.length; i++) {
try {
doubleArray[i] = Double.parseDouble(array[i]);
} catch (NumberFormatException e) {
e.printStackTrace();
doubleArray[i] = 0.0;
}
}
Arrays.sort(doubleArray);
int numbersCount = doubleArray.length;
Double[] finalArray;
if (numbersCount >= 2) {
finalArray = new Double[]{doubleArray[numbersCount - 2], doubleArray[numbersCount - 1]};
} else if (numbersCount == 1) {
finalArray = new Double[]{ doubleArray[0]};
} else {
finalArray = new Double[]{};
}
for (Double number : finalArray) {
System.out.println(number);
}

getIntent().getStringArrayListExtra(NAME) return null if the NAME doesn't equal "Scores"

I have really really strange problem.
Activity4:
protected void ToActivity5() {
Index = 0;
index = 0;
ArrayList<String> scores = new ArrayList<>();
Bundle b = new Bundle();
for(int i = 0; i < Count; i++) {
scores.add(i, Integer.toString((int) (score[i] * 100.0)) + "%");
}
Intent intent = new Intent(Activity4.this, Activity5.class);
b.putStringArrayList("Score", scores);
b.putBoolean("Second", second);
if(!second) {
for(int i = 0; i < tpairs.size(); i++)
for(int j = 0; j < tpairs.get(i).size(); j++)
pairs.add(tpairs.get(i).get(j));
b.putParcelableArrayList("TPairs", pairs);
}
for(int i = 0; i < 7; i++)
score[i] = 0.0;
//if(second)
//finish();
second = true;
intent.putExtras(b);
startActivity(intent);
}
Activity5:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_5);
setTitle(R.string.title_5);
Intent intent = getIntent();
ArrayList<String> percents = intent.getStringArrayListExtra("Score");
ArrayList<TwoStrings> arrayList = new ArrayList<>();
for(int i = 0; i < percents.size(); i++)
arrayList.add(i, new TwoStrings(getResources().getTextArray(R.array.StatisticDays)[i].toString(), percents.get(i)));
//tpairs = getIntent().getParcelableArrayListExtra("TPairs");
//tpairs.size();
//ArrayList<String> ssd = intent.getStringArrayListExtra("Scores");
lv5_1 = (ListView)findViewById(R.id.lv5_1);
TwoStringsAdapter adapter = new TwoStringsAdapter(this, R.layout.layout_5_1, arrayList);
lv5_1.setAdapter(adapter);
}
And the problem is that when I put something with "Score" name then I can get it in the next activity but if the name is different e.g. "Second" or whatever then I can't get the object in Activity5 (I get null object). Please help me. Unfortunately I haven't found any similar problem. That's why I write about the problem here. Thanks for any help!
Use this
Bundle intent = getIntent().getExtras();
ArrayList<String> percents = intent.getStringArrayList("Score");

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