java - Arraylist - 使用 switch 语句添加字符串

标签 java arraylist switch-statement

我是编程新手,我的教授布置了一项作业,要求我们:

"declare on arraylist with the size of 5. Use switch statement to add string values to your arraylist. Retrieve the contents of your arraylist. Check the size of each element. If the element length is less than 8 rerun the program, otherwise count the consonants of each element."

我做了一些研究来理解ArrayList的一些因素; 首先,我这样做了:

import java.util.ArrayList;

public class izeOfArrayList {

  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();

    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");

    int totalElements = arrayList.size();

    System.out.println("ArrayList contains...");
    for(int index=0; index < totalElements; index++)
      System.out.println(arrayList.get(index));

  }
}

此代码仅获取当前存储在我的 ArrayList 中的元素数量,并打印出每个元素。
我有三个问题:

  • 如何使用 switch 语句添加 String 值?
  • 如何检索 ArrayList 的内容?
  • 如何检查 ArrayList 中每个元素的大小?

最佳答案

"declare on arraylist with the size of 5. Use switch statement to add string values to your arraylist. Retrieve the contents of your arraylist. Check the size of each element. If the element length is less than 8 rerun the program, otherwise count the consonants of each element."

让我们逐行解码:

declare on arraylist with the size of 5.

ArrayList<String> myList = new ArrayList<>(5);

我们的 ArrayList 需要定义为 String 列表,因此我们将它们放在尖括号中。构造函数采用起始大小,指定为 5。

Use switch statement to add string values to your arraylist.

完全看不懂。 switch 语句用于控制流程;我们可以根据某些条件决定添加字符串值,但我们无法使用 switch 语句生成输入,并且没有指定条件。以下代码(似乎)对该指令有效:

String values = "values";
switch (values) {
    case "values":
    default:
        myList.add(values);
}

Retrieve the contents of your arraylist.

您已经(大部分)写下了:

int totalElements = myList.size();

for(int index = 0; index < totalElements; index++)
    String tempElem = myList.get(index); //get access to the individual elem

    //here we're going to do something with the current string (probably)

}

Check the size of each element.

我假设通过“每个元素的大小”,您的教授正在寻找每个字符串的长度。

int tempElemLength = tempElem.length();

String 对象有一个 length 方法,它返回一个 int

If the element length is less than 8 rerun the program, otherwise count the consonants of each element.

这虽然乍一看似乎合理,但又令人费解。这是该行的可能解释:

if (tempElemLength < 8) {
    main(null);
} else {
    int tempElemNumConsonants = countConsonants(tempElem);

    //consonants are counted and now what?
}

以下是对当前定义的作业的完整回复:

import java.util.ArrayList;

public class SizeOfArrayList {

    public static void main(String[] args) {
        ArrayList<String> myList = new ArrayList<>(5);

        String values = "values";
        switch (values) {
            case "values":
            default:
                myList.add(values);
        }

        int totalElements = myList.size();

        for (int index = 0; index < totalElements; index++)
            String tempElem = myList.get(index);

            int tempElemLength = tempElem.length();

            if (tempElemLength < 8) {
                main(null);
            } else {
                int tempElemNumConsonants = countConsonants(tempElem);

                //consonants are counted and now what?
                //guess print them out?
                System.out.println('Item ' + index + ': ' + tempElem + ' -> number of consonants: ' + tempElemNumConsonants);
            }
        }
    }
}

这是您的问题的解决方案因为它已被提供;我敢打赌,这并不能解决你的作业问题。

<小时/>

在另一个思想流派中,如果作业的重点是ArrayList的基本使用和理解,并且我是你的教授,那么我的作业< em>打算给我的学生如下:

Declare and ArrayList with the size of 5. Prompt the user for values until they enter 'quit'; use a switch statement to add all String values into the ArrayList that aren't just a number from [0-9]. Loop over each element in the ArrayList; if the length of any String element is less than 8, alert the user then restart the program. If all of the lengths are valid, sum up the consonants of each element. Print out each word and the consonant count, along with a final tally of the number of words along with the total number of consonants.

虽然我知道这不能帮助您解决最初的问题,但我希望它能够帮助您理解教授想要问您的问题。

关于java - Arraylist - 使用 switch 语句添加字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36012171/

相关文章:

java - While 循环没有相应地工作

java - 将 unicode 文件 help.htm 加载到主线程中的 webview 中

java - 我想在 HttpURLConnection android 中发布数组参数

java - 如何在 Java 中存储、排序和打印字符串和 double 的 2D "array"?

java - 为什么 Switch 会跳过一种情况并针对不同情况执行具有错误值的代码

java - 在 Java case 语句中使用变量

java - 嵌套 if/else、开关或更有效的 RPG 战斗?

java - Spring 调度任务 - 只运行一次

java - Netty读取确切的字节数

java - IntelliJ 无法正确识别特定文件,而是作为文本文件卡住