在Android Studio中,代码太大,无法放入一个方法中。

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

Code too large for a method in android studio

问题

作为来自https://stackoverflow.com/a/2407930/8970520的参考,建议使用属性文件来扩展分配的大小。但我不明白如何操作。您能详细说明它是如何工作的吗?提前感谢。

英文:

As a reference from the https://stackoverflow.com/a/2407930/8970520 It was told to use properties file to extend the allocated size .But i couldn't understand how to do that.Could you please elaborate and explain how it works?Thanks in advance.

答案1

得分: 1

在这种情况下,使用属性文件来扩展大小是错误的方法。有些人已经指出了这一点,但你真的非常不希望在源代码中包含数据。如果数据发生变化,你将不得不重新编译你的应用程序并重新部署你的应用程序。

如果数据不经常变化,你可以考虑使用XML文件或SQLite数据库。但是,如果数据发生变化,你肯定需要一种机制来更新它(即使数据很少更改)。也许可以考虑使用Web服务。

如果数据经常变化,你肯定会考虑使用Web服务来下载数据。

英文:

In this case, using the properties file to extend the size is the wrong approach. Some people have already pointed this out, but you really, really don't want to include data in the source code. If the data changes, you would have to recompile your application and do a re-deploy of your app.

If the data does not change frequently, you could consider an XML file or SQLite database. If the data changes, though, you'll definitely need a mechanism to update it (even if the data changes rarely). Perhaps consider a web service.

If the data changes frequently, you'll definitely want to consider a web service to download the data.

答案2

得分: 1

Your code is like this:

public String[] checks(String qww){
	Map<String, String[]> bitmaps  = new HashMap<>();
	String[] cs;
	cs=new String[]{"1", "1", "1", "1", "1", ... 775 more bits ... };
	bitmaps.put("a",cs);
	cs=new String[]{"1", "1", "1", "1", "1", ... 775 more bits ... };
	bitmaps.put("A",cs);
	... 49 more entries ...
	return bitmaps.get(qww);
}

That is just wrong on so many levels:

  1. Storing bits as 1-character strings is a waste of memory, since it uses more than 30 bytes per string, just to store a bit.

    At the very least, you could have stored the bit-characters in a char array, created from a single string, e.g.

    Map<String, char[]> bitmaps  = new HashMap<>();
    bitmaps.put("a", "11111 ... 775 more bits ...".toCharArray());
    bitmaps.put("A", "11111 ... 775 more bits ...".toCharArray());
    ... 49 more entries ...
    return bitmaps.get(qww);
    
  2. The code builds up the entire bitmaps map on every call, then gets one of the map values and discards the map. Waste of processing time.

    Either build the bitmaps map once only, or use a switch statement instead:

    public char[] checks(String qww) {
        switch (qww) {
            case "a": return "11111 ... 775 more bits ...".toCharArray();
            case "A": return "11111 ... 775 more bits ...".toCharArray();
            ... 49 more entries ...
            default: return null;
        }
    }
    
  3. That amount of data should be stored outside the code, e.g. in a property file.

    When loaded into memory, the bits should be stored in a BitSet

So, first create a property file, e.g. checks.bitmaps. The name can be anything, it is just a text file. Content:

a = 111111111111111111111111111111 ... 775 more bits ...
A = 111111111111111111111111111111 ... 775 more bits ...
... 49 more entries ...

Another advantage of this is that the data is so much easier to view and edit.

Change the code as follows:

class Main {
	private static final Map<String, BitSet> bitmaps = new HashMap<>();
	static {
		try (InputStream inStream = Main.class.getResourceAsStream("checks.bitmaps")) {
			Properties props = new Properties();
			props.load(inStream);
			for (String key : props.stringPropertyNames()) {
				char[] bits = props.getProperty(key).toCharArray();
				BitSet bitmap = new BitSet(bits.length);
				for (int i = 0; i < bits.length; i++)
					bitmap.set(i, bits[i] == '1');
				bitmaps.put(key, bitmap);
			}
		} catch (Exception e) {
			throw new IllegalStateException("Bitmap file failed to load: " + e, e);
		}
	}
	public BitSet checks(String qww) {
		return bitmaps.get(qww);
	}
}

The caller can now check for 1-bits as follows:

BitSet bitmap = main.checks(qww);
if (bitmap.get(index)) {
    // Bit is '1'
} else {
    // Bit is '0'
}
英文:

Your code is like this:

public String[] checks(String qww){
	Map&lt;String, String[]&gt; bitmaps  = new HashMap&lt;&gt;();
	String[] cs;
	cs=new String[]{&quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;1&quot;, ... 775 more bits ... };
	bitmaps.put(&quot;a&quot;,cs);
	cs=new String[]{&quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;1&quot;, ... 775 more bits ... };
	bitmaps.put(&quot;A&quot;,cs);
	... 49 more entries ...
	return bitmaps.get(qww);
}

That is just wrong on so many levels:

  1. Storing bits as 1-character strings is a waste of memory, since it uses more than 30 bytes per string, just to store a bit.

    At the very least, you could have stored the bit-characters in a char array, created from a single string, e.g.

    Map&lt;String, char[]&gt; bitmaps  = new HashMap&lt;&gt;();
    bitmaps.put(&quot;a&quot;, &quot;11111 ... 775 more bits ...&quot;.toCharArray());
    bitmaps.put(&quot;A&quot;, &quot;11111 ... 775 more bits ...&quot;.toCharArray());
    ... 49 more entries ...
    return bitmaps.get(qww);
    
  2. The code builds up the entire bitmaps map on every call, then gets one of the map values and discards the map. Waste of processing time.

    Either build the bitmaps map once only, or use a switch statement instead:

    public char[] checks(String qww) {
        switch (qww) {
            case &quot;a&quot;: return &quot;11111 ... 775 more bits ...&quot;.toCharArray();
            case &quot;A&quot;: return &quot;11111 ... 775 more bits ...&quot;.toCharArray();
            ... 49 more entries ...
            default: return null;
        }
    }
    
  3. That amount of data should be stored outside the code, e.g. in a property file.

    When loaded into memory, the bits should be stored in a BitSet

So, first create a property file, e.g. checks.bitmaps. The name can be anything, it is just a text file. Content:

a = 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111000000000111111110000000000000001111000000000000000001110000000000000000001100000000000000000001000111111110000000010111111111110000000011111111111110000000111100000000000000000000000000000000000000000000000000000000000000000000000000000000000111110000000000000111111110000000000001111111100000000000011111110000000000000111111000000000000000111100000000000000000000000000000000000000000000000000000000000000100000001000000000111000000011000000111110000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
A = 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110000000011111111100000000000001111110000000000000000111100000000000000001111000001111100000001110011111111100000011101111111111100000111111111111111000001111111111111110000001111100000000000000011100000000000000000100000000000000000001000000000000000000000000000111111000000000000111111110000000000001111111100000000000011111110000000000000111111100000000000000111100000000010000000000000000000100000000000000000001100000000000100000011110000000111000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
... 49 more entries ...

Another advantage of this is that the data is so much easier to view and edit.

Change the code as follows:

class Main {
	private static final Map&lt;String, BitSet&gt; bitmaps = new HashMap&lt;&gt;();
	static {
		try (InputStream inStream = Main.class.getResourceAsStream(&quot;checks.bitmaps&quot;)) {
			Properties props = new Properties();
			props.load(inStream);
			for (String key : props.stringPropertyNames()) {
				char[] bits = props.getProperty(key).toCharArray();
				BitSet bitmap = new BitSet(bits.length);
				for (int i = 0; i &lt; bits.length; i++)
					bitmap.set(i, bits[i] == &#39;1&#39;);
				bitmaps.put(key, bitmap);
			}
		} catch (Exception e) {
			throw new IllegalStateException(&quot;Bitmap file failed to load: &quot; + e, e);
		}
	}
	public BitSet checks(String qww) {
		return bitmaps.get(qww);
	}
}

The caller can now check for 1-bits as follows:

BitSet bitmap = main.checks(qww);
if (bitmap.get(index)) {
    // Bit is &#39;1&#39;
} else {
    // Bit is &#39;0&#39;
}

huangapple
  • 本文由 发表于 2020年7月21日 20:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63014183.html
匿名

发表评论

匿名网友

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

确定