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 8080357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:23:17+00:00 2026-06-05T16:23:17+00:00

I have an arraylist called fetchContactName() which returns all names (31) in my contact

  • 0

I have an arraylist called fetchContactName() which returns all names (31) in my contact list, an arraylist called fetchContactNumbers() which returns all phone numbers (31) associated with fetchContactName(), and an arraylist called newList which has all the numbers that sent me text messages (6).

I was trying to change the phone numbers in newList by looking for the same numbers in fetchContactNumbers(). When there was a match, I would then index the position in fetchContactNumbers() and use that index to get the name at the same position in fetchContactName(). From there, I would replace the numbers in newList with the names in fetchContactName() according to the indexed position.

This is an example I’ve made that executes as stated above:

http://pastebin.com/pApHNkXa

The problem with the above example is that when I apply it to my listview, only one element in the list is changed to a name. More oddly, newList.get(3) is the only one that changed to a name. Not newList at positions 0, 1, 2, 4 and 5.

From others suggests, how can I use a HashMap to give the same expected result as the example given above? I was told to use fetchContactNumbers() and fetchContactNames() in the Hashmap then use the HashMap to compare with newList, but I cant find any source to achieve this.

UPDATE:

Here is the working code for my HashMap, but the results are still the same as above:

    for (String str : fetchContactNames()) {
        contactNameNew += str + " ";
    }
    for (String str2 : fetchContactNumbers()) {
        contactNumberNew += str2 + " ";
    }

    //String to split
    String temp[];
    String temp2[];
    String delimiter = " ";

    temp = contactNameNew.split(delimiter);
    temp2 = contactNumberNew.split(delimiter);

    HashMap<String, String> contacts = new HashMap<String, String>();
    contacts.put(contactNameNew, contactNumberNew);
    for(int i = 0; i < fetchContactNames().size(); i++){
        contacts.put(temp[i], temp2[i]);
    }

    Iterator<Entry<String, String>> iterator = contacts.entrySet().iterator();

    while(iterator.hasNext()){

        Entry<String, String> entry = iterator.next();
        int position = getPosition(newlist, entry.getValue());
    if (newlist.contains(entry.getValue())) {

        newlist.set(position, entry.getKey());
        }
    }
  • 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-05T16:23:18+00:00Added an answer on June 5, 2026 at 4:23 pm

    I think it will be better if I changed your code and answered…

    import java.util.ArrayList;
    import java.util.HashMap;
    
    public class StringT {
    
    
    public static void main(String[] args) {
    
        // alpha would represent fetchContactNames()
        ArrayList<String> alpha = new ArrayList<String>();
        // bravo would represent fetchContactNumbers()
        HashMap<String, String> bravo = new HashMap<String, String>();
        // charlie would represent newList
        ArrayList<String> charlie = new ArrayList<String>();
    
        bravo.put("1", "One"); charlie.add("5");
        bravo.put("2", "Two"); charlie.add("1");
        bravo.put("3", "Three"); charlie.add("3");
        bravo.put("4", "Four"); charlie.add("4");
        bravo.put("5", "Five"); charlie.add("2");
    
        // this will print charlie just as you see above
        for(int i = 0; i < charlie.size(); i ++){
            System.out.println(charlie.get(i));
        }
    
        System.out.println("---------------------");
    
        // this will print charlie with the numbers replaced as names
        for(int i = 0; i < charlie.size(); i++){
            String value;
            if((value = bravo.get(charlie.get(i))) != null){
                charlie.set(i, value);
            }
        }
    
        for(int i = 0; i < charlie.size(); i ++){
            System.out.println(charlie.get(i));
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList called account which contains Strings. I'm trying to write a
I currently have an arrayList() called players in JavaScript in which I push data
I have an arraylist called 'pri1', I want this array list to be available
I have a string arraylist called names . How do I sort the arraylist
I have a driver class called Advantech which is stored inside an ArrayList .
I have a method which returns an ArrayList of phrases to be used as
Basically, I have a Container class called Employees which has in it an ArrayList.
Hi I have an arraylist of a class I created called Pets which has
At the moment inside the class I have ArrayList which the objects are stored
I have an arraylist which has a class datastructure. public class empRec { public

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.