简体   繁体   中英

What is a more efficient way interchange two strings (alphanumberic part numbers)

I'm relatively new to Android Development and Java. I made an android app for my own personal use because of the inconvenience that they've made interchanging new part numbers with old part numbers.

Long story short, the part numbers in my computer were updated, but they wont change the labels on any of the old part numbers 'until supplies last' (A very long time). The part numbers are in NO LOGICAL ORDER, and are alphanumeric. So they have to be strings (I think? - I'm from a mostly C++ background). We were handed a packet of 10 pages of part numbers to search through every time we have an order to interchange.

So I made an app to interchange the numbers on the computer with the numbers on the label. But that is a huge long list of if/else loops. For example:

else if(str.equals("EUR31"))    {  return "D102 B"; }
    else if(str.equals("EUR228"))   {  return "D222 B"; }
    else if(str.equals("EUR1072"))  {  return "D311 B"; }
    else if(str.equals("ACT482")) {  return "D646 B"; }
    else if(str.equals("ACT325")) {  return "D649 B"; }
    else if(str.equals("EUR394")) {  return "D712 B"; }
    else if(str.equals("ACT526")) {  return "D723 B"; }
    else if(str.equals("EUR391")) {  return "D729 B"; }

The question: Is there a way to optimize this so its not 300 lines of if/else statements? I have looked at hash tables, etc, but if its possible, I'm not sure how to implement it properly. This is merely for myself, it works fast and without any bugs on my phone, just looking to improve.

To solve this kind of logic into programming, HshTable is the best way because it gives O(1) searching time complexity. To implement for your question with the hash table it will look like: create a Map like:

Map<String,String> strMap = new Map<String,String>();
// now put your strings into strMap like
map.put("your_key", "yourValue");
to retrieve the value corresponding to Key it will be like:
if(strMap.containsKey(str)){// your str
return strMap.get(str);
}

Modify it according to your need like in loops and all;

Example : as per your question if you are putting you all string one by one in a map like:

 strMap.put("EUR31","D102 B");
strMap.put("EUR228", "D222 B");
strMap.put("EUR1072","D311 B");
..........
..........

when you retrieve data from map so you can do like:

String str = "EUR31";
if(strMap.containsKey(str)){
return strMap.get(str);
}

you can use 2 ArrayList and checking by index

ArrayList<String> firstList = new ArrayList<String>() {
            {
                add("EUR31");
                add("EUR228");
                add("EUR1072");
                add("ACT482");
            }
        };
ArrayList<String> secondList = new ArrayList<String>() {
            {
                add("D102 B");
                add("D222 B");
                add("D311 B");
                add("D646 B");
            }
};

String getResult(String firstWord) {
     for (int i = 0; i < firstList.size(); i++) {
          if (firstList.get(i).equals(firstWord)) {
              return secondList.get(i);
          }
     }
     return "";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM