如何添加在数组中的元素? [英] How to add elements in array?

查看:118
本文介绍了如何添加在数组中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看code,例如:

public Core()
{   
    class1 = new int[5]; //declares array for 5 class 
    class2 = new int[5]; //delcares array for 5 class

    System.out.println("Please type 1 for First Classe:");
    select = input.nextInt();

    if(select == 1)
    {

    }

}

好吧。如果用户选择的选项号1,占据的一个发生在阵列的存储器。例如:

Alright. If the user select the option number 1, occupy an place of the in the memory of array. For example:

class1 = new int[4];

或者,如果情况超过内存出现一条消息。

Or, if case exceed the memory appears one message.

推荐答案

您不能元素添加到阵列中 - 他们创建后固定大小。我怀疑你会使用列表实施了一些说明,如的ArrayList

You can't add elements to an array - they're a fixed size after creation. I suspect you'd be better off with a List implementation of some description, such as ArrayList.

这不是真的清楚你想要做的,是诚实的东西 - 但它几乎可以肯定,使用一个集合,而不是一个数组会使您的生活更轻松。

It's not really clear what you're trying to do, to be honest - but it's almost certain that using a collection instead of an array would make your life easier.

这篇关于如何添加在数组中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆