可以将更多值添加到完整数组吗? [英] Possible to add more values to a full array?

查看:79
本文介绍了可以将更多值添加到完整数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,您已将数组字的大小设置为10。
调用方法10(字符串字)十次,该方法将10个字添加到数组字中。



<假设用户被问到要添加到数组中的单词数(int n)。
我保存此值,并创建大小为n的数组字。
方法add(String word)被调用n次,以将10个不同的单词添加到数组单词中。
然后询问用户要添加多少个单词(int k)。
然后,将方法add(String word)再调用k次。
但是数组单词已经充满了单词,数组是不可变的,因此我无法在该数组中添加更多单词。
您将如何解决这个问题?
请记住,无法保存用户的第二个值k,我只能访问n,所以我也发现很难创建新的大小为k的数组,因为我不知道大小是多少k是,我无意知道。
我知道可以使用ArrayLists等轻松解决此问题,但是我必须使用数组。



因此,基本上,我需要向数组中添加k个单词大小为n。



到目前为止,我的代码可用于添加前n个单词,但是当涉及添加后k个单词时,我得到ArrayIndexOutOfBoundsException(k)。 。



公共类WordStoreImp
{

  private int计数器= 0; 
私人整数大小;
个私人字串[];

public WordStoreImp(int n)
{
size = n;
words = new String [n];
}

public void add(String word)
{
words [counter] = word;
counter ++;
}

有什么帮助吗?我知道可能甚至不可能btw大声笑

解决方案

您将不得不做类似


的操作

  public void add(String word)
{
if(counter> = words.length){
String [] newWords =新字符串[计数器+ 1];
for(int i = 0; i< words.length; i ++){
newWords [i] = words [i];
}
个单词= newWords;
}
words [counter] = word;
counter ++;
}

但理智的解决方案是使用ArrayList而不是数组


Imagine you have set array words to size 10. You call a method add(String word) ten times which adds 10 words to the array words.

Imagine a user is asked how many words they want to add to an array (int n). I save this value, and create an array words of size n. A method add(String word) is called n times to add 10 different words to the array words. The user is then asked how many more words they want to add (int k). The method add(String word) is then called k more times. But array words is already full of words, and arrays are immutable, so I cant add any more words to that array. How would you go about this problem? Keeping in mind, there is no way of saving the users second value k, I can only access n, so I am also finding it difficult to create a new array of size k, because I don't know what size k is, and I am not meant to know. I know this problem could be easily solved using ArrayLists etc, but I have to use arrays.

So basically, I need to add k more words to array of size n.

My Code so far works for adding the first n words, but when it comes to adding the next k words, I get ArrayIndexOutOfBoundsException(k)...

public class WordStoreImp {

private int counter=0;
private int size;
private String words[];

public WordStoreImp(int n)
{
    size=n;
    words=new String[n];
}

public void add(String word)
{
    words[counter]=word;   
    counter++;
}

any help? I know it may not even be possible btw lol

解决方案

You will have to do something like

public void add(String word)
{
    if (counter >= words.length){
        String[] newWords=new String[counter + 1];
        for(int i=0; i<words.length; i++){
             newWords[i]=words[i];
        }
        words = newWords;
    }
    words[counter]=word;   
    counter++;
}

But the sane solution would be to use a ArrayList and not a array

这篇关于可以将更多值添加到完整数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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