Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8187879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:42:46+00:00 2026-06-07T02:42:46+00:00

I am looping through a JSONArray. names and amount are ArrayList <String> JSONArray data

  • 0

I am looping through a JSONArray. names and amount are ArrayList <String>

JSONArray data = json.getJSONArray("data");
int i = 0;
for (i = 0; i < data.length(); ++i) {
  JSONObject rec = data.getJSONObject(i);
  String name = rec.getString("name");
  names.add(i, name);
  String amount_v = rec.getString("amount");
  amount.add(i, amount_v);
  Log.v(logtag, i + " - " + names.size());
}
Log.v(logtag, "final - " + names.size());

For every value of i, names.size() is twice that of i even though I am adding only one element inside the loop. data has 5 records with name and amount as two columns.
But after the loop, names contain all 10 values (5 names and 5 amounts). But actually, only 5 names should have been added to names list.

What I see in the log..

0 - 2
1 - 4
2 - 6
3 - 8
4 - 10
final - 10

Why does it add the value of amount_v into the names list?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:42:48+00:00Added an answer on June 7, 2026 at 2:42 am

    Because the two references called amount and names refer to the same ArrayList instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to looping through the Json data store and gather all the values.
Per example below...Looping through an object from a parsed JSON string returns an error
If I'm looping through results from a JSON call, how do I know if
I am looping through hundreds of data entries, most of them valid according to
I'm looping through an array of class names in PHP, fetched via get_declared_classes(). How
can I do this without looping through the whole list? List<string> responseLines = new
Looping through each id retrieve value of objects in 1 dimentional array and 2
While looping through a Dataset which code snippet should I use, should I go
When looping through a DataRow and encountering types such as DataRow dr; dr[someString] dr[someInteger]
I've been looping through the contents of a list in the simple fashion to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.