理解遍历字符数组的URL缩短代码

huangapple go评论86阅读模式
英文:

Understanding URL shortening code that iterators over a char array

问题

以下是翻译好的部分:

作为学习练习,我正在开发一个URL缩短工具,它可以接受一个URL并将其缩短,类似于 https://tinyurl.com/

这段代码:

import java.util.HashMap;
import java.util.Random;

/*
 * URL缩短工具
 *
 * https://gist.github.com/rakeshsingh/64918583972dd5a08012
 *
 */
public class URLShortener {

    // 存储生成的键(key)的地方
    private HashMap<String, String> keyMap; // 键-URL映射

    private char myChars[]; // 该数组用于字符到数字的映射
    private Random myRand; // 用于生成随机整数的Random对象
    private int keyLength;

    // 构造函数,允许定义缩短的URL键长度和基本URL名称
    public URLShortener(int keyLength) {
        this.setup();
        this.keyLength = keyLength;
    }

    private void setup(){
        keyMap = new HashMap<String, String>();
        myRand = new Random();
        myChars = new char[62];
        for (int i = 0; i < 62; i++) {
            int j = 0;
            if (i < 10) {
                j = i + 48;
            } else if (i > 9 && i <= 35) {
                j = i + 55;
            } else {
                j = i + 61;
            }
            myChars[i] = (char) j;
        }
    }

    // shortenURL
    // 可以调用的公共方法,用于缩短给定的URL
    public String shortenURL(String longURL) {
        String key = "";

        for (int i = 0; i <= keyLength; i++) {
            key += myChars[myRand.nextInt(62)];
        }

        return key;
    }

    // generateKey
    // 为什么上界是62?
    private String generateKey() {
        String key = "";
        boolean flag = true;
        for (int i = 0; i <= keyLength; i++) {
            key += myChars[myRand.nextInt(62)];
        }
        return key;
    }

    // 测试代码
    public static void main(String args[]) {
        URLShortener u = new URLShortener(5);
        String urls[] = { "www.google.com/", "www.google.com",
                "http://www.yahoo.com", "www.yahoo.com/",
                "www.icicibank.com"
                , "www.icicibank.com"};

        for (int i = 0; i < urls.length; i++) {
            System.out.println("URL:" + urls[i] + "\tTiny: "
                    + u.shortenURL(urls[i]));
        }
    }
}

输出:

URL:www.google.com/	Tiny: wx4oPv
URL:www.google.com	Tiny: S2JT01
URL:http://www.yahoo.com	Tiny: jFOksR
URL:www.yahoo.com/	Tiny: EYcQ8o
URL:www.icicibank.com	Tiny: V5JOn3
URL:www.icicibank.com	Tiny: u4xQzn

上述代码是从 https://gist.github.com/rakeshsingh/64918583972dd5a08012 进行了重构。

我试图理解特别是这段代码块:

private void setup(){
    keyMap = new HashMap<String, String>();
    myRand = new Random();
    myChars = new char[62];
    for (int i = 0; i < 62; i++) {
        int j = 0;
        if (i < 10) {
            j = i + 48;
        } else if (i > 9 && i <= 35) {
            j = i + 55;
        } else {
            j = i + 61;
        }
        myChars[i] = (char) j;
    }
}

为什么要在一个长度为62的字符数组上进行迭代,在位置 i < 10 处将当前字符位置加上48,在位置 (i > 9 && i <= 35) 处将当前字符位置加上55,对于其他所有位置都加上61?

英文:

As a learning exercise I 'm working on a URL shortener that takes a URL and shortens it, similar to https://tinyurl.com/

This code:

import java.util.HashMap;
import java.util.Random;
/*
* URL Shortener
*
* https://gist.github.com/rakeshsingh/64918583972dd5a08012
*
*/
public class URLShortener {
// storage for generated keys
private HashMap&lt;String, String&gt; keyMap; // key-url map
private char myChars[]; // This array is used for character to number
// mapping
private Random myRand; // Random object used to generate random integers
private int keyLength;
// Constructor which enables you to define tiny URL key length and base URL
// name
public URLShortener(int keyLength) {
this.setup();
this.keyLength = keyLength;
}
private void setup(){
keyMap = new HashMap&lt;String, String&gt;();
myRand = new Random();
myChars = new char[62];
for (int i = 0; i &lt; 62; i++) {
int j = 0;
if (i &lt; 10) {
j = i + 48;
} else if (i &gt; 9 &amp;&amp; i &lt;= 35) {
j = i + 55;
} else {
j = i + 61;
}
myChars[i] = (char) j;
}
}
// shortenURL
// the public method which can be called to shorten a given URL
public String shortenURL(String longURL) {
String key = &quot;&quot;;
for (int i = 0; i &lt;= keyLength; i++) {
key += myChars[myRand.nextInt(62)];
}
return key;
}
// generateKey
//Why is upper bound 62?
private String generateKey() {
String key = &quot;&quot;;
boolean flag = true;
for (int i = 0; i &lt;= keyLength; i++) {
key += myChars[myRand.nextInt(62)];
}
return key;
}
// test the code
public static void main(String args[]) {
URLShortener u = new URLShortener(5);
String urls[] = { &quot;www.google.com/&quot;, &quot;www.google.com&quot;,
&quot;http://www.yahoo.com&quot;, &quot;www.yahoo.com/&quot;,
&quot;www.icicibank.com&quot;
, &quot;www.icicibank.com&quot;};
for (int i = 0; i &lt; urls.length; i++) {
System.out.println(&quot;URL:&quot; + urls[i] + &quot;\tTiny: &quot;
+ u.shortenURL(urls[i]));
}
}
}

prints:

URL:www.google.com/	Tiny: wx4oPv
URL:www.google.com	Tiny: S2JT01
URL:http://www.yahoo.com	Tiny: jFOksR
URL:www.yahoo.com/	Tiny: EYcQ8o
URL:www.icicibank.com	Tiny: V5JOn3
URL:www.icicibank.com	Tiny: u4xQzn

The above code is refactored from https://gist.github.com/rakeshsingh/64918583972dd5a08012

I'm trying to understand this code block in particular:

private void setup(){
keyMap = new HashMap&lt;String, String&gt;();
myRand = new Random();
myChars = new char[62];
for (int i = 0; i &lt; 62; i++) {
int j = 0;
if (i &lt; 10) {
j = i + 48;
} else if (i &gt; 9 &amp;&amp; i &lt;= 35) {
j = i + 55;
} else {
j = i + 61;
}
myChars[i] = (char) j;
}
}

Why iterate over a char array of 62 elements and at location i &lt; 10 add 48 to the current char position, at location (i &gt; 9 &amp;&amp; i &lt;= 35) and 55 to the current char position and for all other positions add 61 ?

答案1

得分: 1

你可能想查看ASCII表。数字0从索引48开始,字母A从65开始(即55+10),字母a从97开始(即61+36)。这只是一种用于初始化myChars数组以使用数字和字母的花式方法。

英文:

You might want to look at the ASCII table. The digit 0 start at index 48, the letter A at 65 (which is 55+10) and the letter a at 97 (which is 61+36). This is just a fancy way to initialize the myChars array with the digits and letters to use.

答案2

得分: 1

好的,以下是您要翻译的内容:

似乎 myChars 数组只是从索引到字母数字字符的映射。

26 个拉丁小写字母加上 26 个大写字母加上 10 个数字等于 62。因此数组的长度为 62。

现在,索引 0 到 9 对应于从 '0' 到 '9' 的数字。但是,将 '0' 打印为整数会得到 48。这就是为什么要在 0 到 9 之间加上 48,以得到字符 '0'(直到 '9')。

对其他字符也是同样的道理:

将字符 'A' 打印为整数得到 65,因此在索引 10 到 35(记住 35 是 10+26-1,即所有拉丁大写字母字符)处需要加上 55。

类似地,将字符 'a' 打印为整数得到 97,因此在索引 36 到 61(记住 61 是 36+26-1,即所有拉丁小写字母字符)处需要加上 61。

您还可以看一下:
https://stackoverflow.com/questions/7019504/in-what-encoding-is-a-java-char-stored-in

英文:

Seems like the myChars array is just a map from indices to alphanumeric characters.

26 latin lower case characters plus 26 upper case characters plus 10 digits equals 62. Hence the length of the array.

Now, index 0 to 9 corresponds to a digit from '0' to '9'. But, printing '0' as an integer yields 48. That's why you add 48 to 0 (up to 9) to get the character '0' (up to '9').

Same thing as for the others:

Printing the character 'A' as an integer yields 65, hence you need to add 55 at the index 10 to 35 (remember 35 is 10+26-1, ie all the latin upper case characters).

Similarly, printing the character 'a' as an integer yields 97, hence you need to add 61 at the index 36 to 61 (remember 61 is 36+26-1, ie all the latin lower case characters).

You may take a look also at:
https://stackoverflow.com/questions/7019504/in-what-encoding-is-a-java-char-stored-in

huangapple
  • 本文由 发表于 2020年8月30日 04:54:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63651710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定