简体   繁体   English

删除数组的第一个元素而不使用 java

[英]Removing the first element of an array without using in java

i'm trying to read a line in a file and store it in an array, after doing this i want to remove the first element in the array so that the length can be 1 element less and make the length of the array same as another array so i can match both arrays using for loop.我正在尝试读取文件中的一行并将其存储在一个数组中,这样做之后我想删除数组中的第一个元素,以便长度可以减少 1 个元素并使数组的长度与另一个相同数组,所以我可以使用 for 循环匹配两个 arrays。 the line i want to read from the file is "London 2 7 24 16 -15 8 27"我想从文件中读取的行是“London 2 7 24 16 -15 8 27”

try {
        File myObj = new File("temperatures.txt");
        FileReader read = new FileReader(myObj);
        Scanner myReader = new Scanner(myObj);
        Scanner input = new Scanner(System.in);
        LineNumberReader lines = new LineNumberReader(read);
        lines.skip(Long.MAX_VALUE);
        int len = lines.getLineNumber();
        int count = len;
        String ans;
        for (int i = 0; i < len && myReader.hasNextLine();i++){
            System.out.println(count+" Cities temperature left!");
            System.out.print("Do you want to check temperature details; enter y or n: ");
            ans = input.nextLine();
            if(ans.equals("y")){
                String[] getData = myReader.nextLine().split(" ");
                String[] days = {"Mon", "Tue", "Weds", "Thurs", "Fri", "Sat", "Sun"};
                for (int j = 0; j < getData.length; j++){
                    System.out.println(days[j]+": "+getData[j]);
                }

            }
            else{

            }
        }
    } catch (FileNotFoundException e) {
        System.out.println("An error occurred.");
        e.printStackTrace();
  }

Why not add +1 to the index value of getData[] in the loop?为什么不在循环中的 getData[] 的索引值上加 +1 呢?

System.out.println(days[j] + ": " +getData[j+1]);

There are several possible solutions to your problem.您的问题有几种可能的解决方案。

  1. Not deleting the element at all根本不删除元素

You can iterate 2 arrays in parallel like this:您可以像这样并行迭代 2 arrays:

for (int j = 0; j < days.length; j++) {
    System.out.println(days[j] + ": " + getData[j + 1]);
}

As you can see, we start and index 0 with days , but we start at 1 with getData .如您所见,我们从 0 开始并索引days ,但我们从 1 开始使用getData This will work, because you know that getData is exactly one element bigger than days .这将起作用,因为您知道getData恰好是比days大的一个元素。 Also, note that the condition of the loop changed to j < days.length .另外,请注意循环的条件更改为j < days.length This is because days is shorter and so we should iterate through the shorter array.这是因为days更短,所以我们应该遍历更短的数组。 Alternatively, you can write j < getData.length - 1 .或者,您可以编写j < getData.length - 1

  1. Creating a copy创建副本

There is no way to delete an element from an array.无法从数组中删除元素。 You have to construct a new array.您必须构造一个新数组。

String[] newData = new String[getData.length - 1];
for (int j = 0; j < newData.length; j++) {
    newData[j] = getData[j + 1];
}

Then you can use newData instead of getData to print the numbers.然后您可以使用newData而不是getData来打印数字。

  1. Use a collection or a stream使用集合或 stream

I won't go into the details, but you can construct a collection (eg an ArrayList) from your array, delete the first element from the list (because unlike arrays, you can actually delete elements of lists), and then iterate the list (no need to create another array).我不会 go 详细介绍,但是您可以从数组中构造一个集合(例如 ArrayList),从列表中删除第一个元素(因为与 arrays 不同,您实际上可以删除列表的元素),然后迭代列表(无需创建另一个数组)。 Or, you can create a stream from an array, skip the first element (streams have the skip method), and copy the stream into an array.或者,您可以从数组创建 stream,跳过第一个元素(流具有skip方法),然后将 stream 复制到数组中。

But as for me, this way is overkill for such a simple task.但就我而言,这种方式对于如此简单的任务来说太过分了。 I think that even creating a copy is a bit too much.我认为即使创建副本也有点太多了。 You're better off just using the first solution, it's the easiest one.你最好只使用第一个解决方案,这是最简单的一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM