移除数组中第一个元素的方法,不使用 `in java`

huangapple go评论85阅读模式
英文:

Removing the first element of an array without using in java

问题

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 + " 城市剩余温度信息!");
        System.out.print("是否要查看温度详情?输入 y 或 n: ");
        ans = input.nextLine();
        if (ans.equals("y")) {
            String[] getData = myReader.nextLine().split(" ");
            String[] days = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
            for (int j = 0; j < getData.length; j++) {
                System.out.println(days[j] + ": " + getData[j]);
            }
        } else {

        }
    }
} catch (FileNotFoundException e) {
    System.out.println("发生错误。");
    e.printStackTrace();
}
英文:

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. the line i want to read from the file is
"London 2 7 24 16 -15 8 27"

try {
        File myObj = new File(&quot;temperatures.txt&quot;);
        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 &lt; len &amp;&amp; myReader.hasNextLine();i++){
            System.out.println(count+&quot; Cities temperature left!&quot;);
            System.out.print(&quot;Do you want to check temperature details; enter y or n: &quot;);
            ans = input.nextLine();
            if(ans.equals(&quot;y&quot;)){
                String[] getData = myReader.nextLine().split(&quot; &quot;);
                String[] days = {&quot;Mon&quot;, &quot;Tue&quot;, &quot;Weds&quot;, &quot;Thurs&quot;, &quot;Fri&quot;, &quot;Sat&quot;, &quot;Sun&quot;};
                for (int j = 0; j &lt; getData.length; j++){
                    System.out.println(days[j]+&quot;: &quot;+getData[j]);
                }

            }
            else{

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

答案1

得分: 0

为什么不在循环中的索引值getData[]后加上+1呢?

System.out.println(days[j] + &quot;: &quot; +getData[j+1]);
英文:

Why not add +1 to the index value of getData[] in the loop?

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

答案2

得分: 0

以下是您的问题的翻译部分:

有几种可能的解决方案可以解决您的问题。

  1. 完全不删除该元素

您可以像这样并行迭代两个数组:

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

正如您所见,我们从0开始索引days,但从1开始索引getData。这样可以工作,因为您知道getDatadays多一个元素。还要注意循环的条件已更改为j < days.length。这是因为days较短,所以我们应该遍历较短的数组。或者,您可以编写j < getData.length - 1

  1. 创建副本

无法从数组中删除元素。您必须构造一个新数组。

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

然后您可以使用newData来打印数字,而不是使用getData

  1. 使用集合或流

我不会详细介绍,但您可以从数组构造一个集合(例如ArrayList),从列表中删除第一个元素(因为与数组不同,您实际上可以删除列表的元素),然后迭代该列表(无需创建另一个数组)。或者,您可以从数组创建一个流,跳过第一个元素(流具有skip方法),并将流复制到数组中。

但对我来说,对于如此简单的任务来说,这种方法过于繁琐。我认为甚至创建副本都有点过多。您最好只使用第一种解决方案,这是最简单的方法。

英文:

There are several possible solutions to your problem.

  1. Not deleting the element at all

You can iterate 2 arrays in parallel like this:

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

As you can see, we start and index 0 with days, but we start at 1 with getData. This will work, because you know that getData is exactly one element bigger than days. Also, note that the condition of the loop changed to j &lt; days.length. This is because days is shorter and so we should iterate through the shorter array. Alternatively, you can write j &lt; 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 &lt; newData.length; j++) {
	newData[j] = getData[j + 1];
}

Then you can use newData instead of getData to print the numbers.

  1. Use a collection or a stream

I won't go into the details, but you can construct a collection (e.g. 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). 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.

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.

huangapple
  • 本文由 发表于 2020年4月11日 00:41:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/61144726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定