-1

So, hello guys. I apologize, if that question has been answered, but I found nothing for my question.

Imagine, that there is a guy, who is entering his name into console, and system looks for his name in array "names". If his name is in array, system printin that yeah and send it to FBI, haha. But if there is no his name in array, system printing that There is no entered name, And then, it's needed to add entered name to array. For the FBI-base be wider) Here is my code, and i works, if there is entered name in array. But it shows exception, of there is no name.

Thank's for help!)

import java.util.Arrays;

import java.util.List;
import java.util.Scanner;

public class FBI {
    String[] names = {"Вася", "Денис", "Петя", "Анатолий", "Евгений", "Саша", "Дима"};
    String s;
    Scanner scn = new Scanner(System.in);
    List<String> list = Arrays.asList(names);



    void Ss(String s2){
        System.out.println("Введите имя:");
        s2 = scn.nextLine();    
        s = s2;
    }

    public void IsThereIn(){
        if(list.contains(s)){
            System.out.println("Есть такой в базе!");
            FBI fb = new FBI();
            fb.SendToFBI();
            System.out.println("DONE!");
        }else{
            System.out.println("Такого нет!");
            list.add(s);

        }

    }
    public void SendToFBI(){
        System.out.println("Sending to FBI");
        for(int i = 1;i <=5;i++){
            System.out.println("На счёт " + i);
        }
    }


}
1

2 Answers 2

1

Arrays.asList creates a List view of the array. The only way you can modify the content of this list without getting an exception is by using the set method (or some other method that just replace element(s)). You cannot increase the size of the list.

If you want a list that allows you to add new elements, you should use a ArrayList:

List<String> list = new ArrayList<>(Arrays.asList(names));

Arrays do not support resizing (see Java dynamic array sizes?): If you need to "modify" the length of a array, you need to replace it with a new one, which can be done with this code:

names = list.toArray(new String[list.size()]);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank's. That is useful and now code is showing no exceptions, but the lenght of array is still 7. Where is added string?I thought, that my array would get new String and it's lenght would be bigger. If at start the lenght of array was 7, it should be 8?No?
@LameLane You cannot resize arrays (Which is also the reason the add operation is not supported in the list returned by Arrays.asList). I'd use the List everywhere instead, but I added some code to create a new array containing the elements in the List.
Thanks, you helped me with your advise;)
1

It is not possible to add values to ArrayList created by using Arrays.asList(array)

Click on the link to see source code

List<String> list = Arrays.asList(names);

what you can do is make new copy of ArrayList and them all

i.e

List<String> list= new ArrayList<String>(list);

2 Comments

I downvoted it earlier because reason "it doesn't have add methods" was simply wrong. It does have these methods but because arrays can't be resized it doesn't support them. I didn't leave comment earlier because I was looking for proper duplicate first (sorry for delay).
@Pshemo I mean to add something like It doesn't have add method implementations but I have missed. Also mean they have not provided implementations because the arrays are not resizable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.