ArrayList的泛型使用方法有哪些?

2026-04-28 02:081阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计460个文字,预计阅读时间需要2分钟。

ArrayList的泛型使用方法有哪些?

面试题 + 02.06. 回文链表

ArrayList的泛型使用方法有哪些?

在这个题目中,我使用ArrayList来存储链表中的值,然后使用双指针来判断是否为回文。但是,由于测试用例中存在超过[-128, 127]范围的数字,并且由于泛型类型的使用导致使用了Integer类型,这导致出现了问题。

面试题 02.06. 回文链表这个题目中我是用ArrayList存储链表中的值,然后使用双指针来判断是否为回文

但是测试用例中存在有超过[-128,127]的数字,由于泛型类型使用到了Integer,由于Integer是包装类型,但是没有使用equals方法比较,所以出现-129!=-129的问题

观察Integer源码

  • valueOf方法:

1 public static Integer valueOf(int i) { 2 if (i >= IntegerCache.low && i <= IntegerCache.high) 3 return IntegerCache.cache[i + (-IntegerCache.low)]; 4 return new Integer(i); 5 }

  • 内部缓存IntegerCache数组:

1 private static class IntegerCache { 2 static final int low = -128; 3 static final int high; 4 static final Integer cache[]; 5 6 static { 7 // high value may be configured by property 8 int h = 127; 9 String integerCacheHighPropValue = 10 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); 11 if (integerCacheHighPropValue != null) { 12 try { 13 int i = parseInt(integerCacheHighPropValue); 14 i = Math.max(i, 127); 15 // Maximum array size is Integer.MAX_VALUE 16 h = Math.min(i, Integer.MAX_VALUE - (-low) -1); 17 } catch( NumberFormatException nfe) { 18 // If the property cannot be parsed into an int, ignore it. 19 } 20 } 21 high = h; 22 23 cache = new Integer[(high - low) + 1]; 24 int j = low; 25 for(int k = 0; k < cache.length; k++) 26 cache[k] = new Integer(j++); 27 28 // range [-128, 127] must be interned (JLS7 5.1.7) 29 assert IntegerCache.high >= 127; 30 }

结论:

  在范围内使用缓存,当在缓存范围内使用“==”比较,比较缓存数组中的地址,自然相等。

  在范围外比较,new Integer对象,使用“==”比较包装类,自然地址不等。

本文共计460个文字,预计阅读时间需要2分钟。

ArrayList的泛型使用方法有哪些?

面试题 + 02.06. 回文链表

ArrayList的泛型使用方法有哪些?

在这个题目中,我使用ArrayList来存储链表中的值,然后使用双指针来判断是否为回文。但是,由于测试用例中存在超过[-128, 127]范围的数字,并且由于泛型类型的使用导致使用了Integer类型,这导致出现了问题。

面试题 02.06. 回文链表这个题目中我是用ArrayList存储链表中的值,然后使用双指针来判断是否为回文

但是测试用例中存在有超过[-128,127]的数字,由于泛型类型使用到了Integer,由于Integer是包装类型,但是没有使用equals方法比较,所以出现-129!=-129的问题

观察Integer源码

  • valueOf方法:

1 public static Integer valueOf(int i) { 2 if (i >= IntegerCache.low && i <= IntegerCache.high) 3 return IntegerCache.cache[i + (-IntegerCache.low)]; 4 return new Integer(i); 5 }

  • 内部缓存IntegerCache数组:

1 private static class IntegerCache { 2 static final int low = -128; 3 static final int high; 4 static final Integer cache[]; 5 6 static { 7 // high value may be configured by property 8 int h = 127; 9 String integerCacheHighPropValue = 10 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); 11 if (integerCacheHighPropValue != null) { 12 try { 13 int i = parseInt(integerCacheHighPropValue); 14 i = Math.max(i, 127); 15 // Maximum array size is Integer.MAX_VALUE 16 h = Math.min(i, Integer.MAX_VALUE - (-low) -1); 17 } catch( NumberFormatException nfe) { 18 // If the property cannot be parsed into an int, ignore it. 19 } 20 } 21 high = h; 22 23 cache = new Integer[(high - low) + 1]; 24 int j = low; 25 for(int k = 0; k < cache.length; k++) 26 cache[k] = new Integer(j++); 27 28 // range [-128, 127] must be interned (JLS7 5.1.7) 29 assert IntegerCache.high >= 127; 30 }

结论:

  在范围内使用缓存,当在缓存范围内使用“==”比较,比较缓存数组中的地址,自然相等。

  在范围外比较,new Integer对象,使用“==”比较包装类,自然地址不等。