Integer [] to int[]

solution1:

The easiest way to do this is to make use of Apache Commons Lang. It has a handy ArrayUtils class that can do what you want. Use the toPrimitive method with the overload for an array of Integers.

List<Integer> myList;
 ... assign and fill the list
int[] intArray = ArrayUtils.toPrimitive(myList.toArray(new Integer[myList.size()]));

This way you don't reinvent the wheel. Commons Lang has a great many useful things that Java left out. Above, I chose to create an Integer list of the right size. You can also use a 0-length static Integer array and let Java allocate an array of the right size:

static final Integer[] NO_INTS = new Integer[0];
   ....
int[] intArray2 = ArrayUtils.toPrimitive(myList.toArray(NO_INTS))

solution2:  google's guava
	ImmutableList<Integer> list = ImmutableList.of(23,0,23,1,-9,234,33);
        int [] ef =com.google.common.primitives.Ints.toArray(list);
        --------------------------------------------------------------------------------
         List<Integer> lst = Lists.newArrayList();
         lst.add(Integer.valueOf(12));
         lst.add(Integer.valueOf(132));
         lst.add(Integer.valueOf(3));
         lst.add(Integer.valueOf(2));
        int [] ff =com.google.common.primitives.Ints.toArray(lst);
       System.out.println("ff:"+com.google.common.primitives.Ints.max(ff));
       System.out.println("ff:"+com.google.common.primitives.Ints.min(ff));
	List<Integer>  tst = Lists.newArrayList();
        tst.add(1);
        tst.add(-1);
        tst.add(45);
        
        Integer[] tt = tst.toArray(new Integer[]{});
        //Integer[] tt = tst.toArray(new Integer[tst.size()]);
        int [] ef =com.google.common.primitives.Ints.toArray(tst);
        System.out.println("ef:"+com.google.common.primitives.Ints.max(ef));
	@Test
	public void testArray(){
		ImmutableList<String> params = ImmutableList.of("jgmc", "dlzh","dataToken");
		String [] a=params.toArray(new String[params.size()]);
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}
	}
	
//	jgmc
//	dlzh
//	dataToken
### intinteger 的区别及其在不同编程语言中的用法 #### 定义与概念 `int` 是许多编程语言中用于表示整数类型的关键词。而 `integer` 通常也是指代同样的含义,在某些上下文中可能作为更正式的说法来描述这一数据类型。 #### 编程语言差异 ##### C/C++ 在C++ 中,`int` 表示一种基本的数据类型,用来存储有符号的整数值[^4]。它通常是机器字长的一半大小,但在现代计算机上一般占用32位或64位空间取决于编译器设置和平台架构。值得注意的是,这里并没有直接使用 `integer` 这样的关键字;相反,程序员会通过文档或其他方式提及这种类型为 “integer”。 ```cpp #include <iostream> int main() { int num = 10; std::cout << "The value of num is: " << num << '\n'; } ``` ##### Java Java 使用 `int` 关键词定义一个固定宽度(32位)的带符号整型变量。同样地,虽然有时人们可能会提到 `integer` 来泛指这类对象,但实际上当涉及到具体语法时还是应该采用 `int` 或者其封装类 `Integer`[^5]。 ```java public class Main { public static void main(String[] args) { int number = 20; System.out.println("Number: " + number); Integer objNum = new Integer(20); // Using wrapper class System.out.println("Object Number: " + objNum); } } ``` ##### Python Python 并不区分 `int` 和 `integer`,二者可以互换使用,并且该语言支持任意精度的整数运算[^6]。这意味着除非内存不足,否则不会遇到溢出错误。此外,Python 自动处理大整数转换成更高效率的形式来进行计算。 ```python num = 10 ** 100 # This will work without any issues due to arbitrary precision integers. print(f"The large number {num} has been successfully assigned.") ``` ##### SQL SQL 数据库查询语言中有专门针对列定义使用的 `INTEGER` 类型,它可以接受一定范围内的正负整数,默认情况下可能是32位长度,不过这依赖于具体的数据库管理系统实现细节[^7]。 ```sql CREATE TABLE example ( id INTEGER PRIMARY KEY, name VARCHAR(50) ); INSERT INTO example VALUES (1, 'Alice'); SELECT * FROM example WHERE id=1; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值