जावा केवल वास्तव में एकल आयामी सरणी लागू करता है। इसमें बहु-आयामी प्रकार हैं, हालांकि दो आयामी सरणी वास्तव में सरणी की सरणी के रूप में लागू की जाती हैं। प्रत्येक सरणी में लगभग 16 बाइट्स का ओवरहेड होता है। ओवरहेड को कम करने के लिए आप int[2][x]
के साथ बेहतर हैं।
आप इस समस्या को पूरी तरह से सहायक तरीकों का उपयोग करके टालना कर सकते हैं।
final int[] array = new int[2 * veryLargeNumber];
public int get(int x, int y) {
return array[idx(x, y)];
}
public void set(int x, int y, int val) {
array[idx(x, y)] = val;
}
private int idx(int x, int y) {
return x * 2 + y; // or x * veryLargeNumber + y;
}
एक अनूठा प्रत्येक वस्तु हैश खुद को यह प्रदान करते हैं, जो अपनी वस्तु शीर्षक में संग्रहीत किया जाता है hashCode उत्पन्न करने के लिए।
आप http://ideone.com/oGbDJ0 से देख सकते हैं कि प्रत्येक नेस्टेड सरणी स्वयं में एक वस्तु है।
int[][] array = new int[20][2];
for (int[] arr : array) {
System.out.println(arr);
}
एक int[]
जो [I
@
hashCode() शीर्षक में संग्रहीत के बाद उसके बाद के आंतरिक प्रतिनिधित्व प्रिंट करता है। यह कुछ विश्वास नहीं है, वस्तु का पता। पता, hashCode के रूप में इस्तेमाल नहीं किया जा सकता के रूप में वस्तु जीसी द्वारा किसी भी समय ले जाया जा सकता है (जब तक आप एक JVM जो वस्तुओं कभी नहीं ले जाता है)
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
[[email protected]
आप देख सकते हैं कितना स्मृति अगर प्रयोग किया जाता है आप -XX:-UseTLAB
https://github.com/peter-lawrey/Performance-Examples/blob/master/src/main/java/vanilla/java/memory/ArrayAllocationMain.java
public static void main(String[] args) {
long used1 = memoryUsed();
int[][] array = new int[200][2];
long used2 = memoryUsed();
int[][] array2 = new int[2][200];
long used3 = memoryUsed();
if (used1 == used2) {
System.err.println("You need to turn off the TLAB with -XX:-UseTLAB");
} else {
System.out.printf("Space used by int[200][2] is " + (used2 - used1) + " bytes%n");
System.out.printf("Space used by int[2][200] is " + (used3 - used2) + " bytes%n");
}
}
public static long memoryUsed() {
Runtime rt = Runtime.getRuntime();
return rt.totalMemory() - rt.freeMemory();
}
प्रिंट
Space used by int[200][2] is 5720 bytes
Space used by int[2][200] is 1656 bytes
ऐसा लगता है कि पहला विकल्प कम ओवरहेड होगा, क्योंकि यह दो बड़े 1 डी सरणी के बराबर है, जबकि दूसरा विकल्प बड़ी संख्या में छोटे 1 डी सरणी के बराबर है। पहले विकल्प को कम सरणी उदाहरणों की आवश्यकता होती है - 2 डी सरणी और 2 1-डी सरणी। दूसरे विकल्प के लिए 2 डी सरणी उदाहरण और कई 1-डी सरणी उदाहरणों की आवश्यकता होती है। – Eran
मुझे विश्वास नहीं है कि एक है, नहीं। वहाँ होगा अगर दूसरा आयाम निर्दिष्ट नहीं किया गया था, लेकिन यहां आप इसे निर्दिष्ट करते हैं। – fge
@ वास्तव में ईरान नहीं; यह 'multianewarray' है जिसका उपयोग तब किया जाता है जब आपके पास "निश्चित आयाम" एकाधिक सरणी – fge