英文:
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("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();
}
答案1
得分: 0
为什么不在循环中的索引值getData[]后加上+1呢?
System.out.println(days[j] + ": " +getData[j+1]);
英文:
Why not add +1 to the index value of getData[] in the loop?
System.out.println(days[j] + ": " +getData[j+1]);
答案2
得分: 0
以下是您的问题的翻译部分:
有几种可能的解决方案可以解决您的问题。
- 完全不删除该元素
您可以像这样并行迭代两个数组:
for (int j = 0; j < days.length; j++) {
System.out.println(days[j] + ": " + getData[j + 1]);
}
正如您所见,我们从0开始索引days
,但从1开始索引getData
。这样可以工作,因为您知道getData
比days
多一个元素。还要注意循环的条件已更改为j < days.length
。这是因为days
较短,所以我们应该遍历较短的数组。或者,您可以编写j < getData.length - 1
。
- 创建副本
无法从数组中删除元素。您必须构造一个新数组。
String[] newData = new String[getData.length - 1];
for (int j = 0; j < newData.length; j++) {
newData[j] = getData[j + 1];
}
然后您可以使用newData
来打印数字,而不是使用getData
。
- 使用集合或流
我不会详细介绍,但您可以从数组构造一个集合(例如ArrayList),从列表中删除第一个元素(因为与数组不同,您实际上可以删除列表的元素),然后迭代该列表(无需创建另一个数组)。或者,您可以从数组创建一个流,跳过第一个元素(流具有skip
方法),并将流复制到数组中。
但对我来说,对于如此简单的任务来说,这种方法过于繁琐。我认为甚至创建副本都有点过多。您最好只使用第一种解决方案,这是最简单的方法。
英文:
There are several possible solutions to your problem.
- Not deleting the element at all
You can iterate 2 arrays in parallel like this:
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
. 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 < days.length
. This is because days
is shorter and so we should iterate through the shorter array. Alternatively, you can write j < getData.length - 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.
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论