我有一个字符串数组,我只需要那些以"81"或"82"开头的条目.我试过这样的:
var lines = File.ReadAllLines(fileName); // This returns an array of strings
lines = lines.TakeWhile(item => item.StartsWith("81") ||item.StartsWith("82")).ToArray();
Run Code Online (Sandbox Code Playgroud)
但这只是行不通.它返回一个空字符串数组.
当我使用for循环遍历行并每次进行比较时
if (!firstline.Substring(0, 2).StartsWith("81")) continue;
Run Code Online (Sandbox Code Playgroud)
然后我拿了所需的条目,它工作得很好.
有任何建议如何使用LINQ正确吗?
假设我有一个类似以下的类:
class Book
{
public int id;
public string title;
}
Run Code Online (Sandbox Code Playgroud)
在某个地方之后我有一个数组Book[] books,现在要一个标题数组string[] titles = {books[0].title, books[1].title, ..., books[n].title}。有没有比遍历books数组更简单的方法?就像是
string[] titles = books.getProperty(title)
Run Code Online (Sandbox Code Playgroud)
提前致谢
所以我有一个数组,我正在尝试从中获取一个子数组。
x = np.arange(0,100).reshape((10, 10))
Run Code Online (Sandbox Code Playgroud)
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, …Run Code Online (Sandbox Code Playgroud) 在子数组上使用Java Arrays.sort()时,我注意到了奇怪的行为.这是一个演示程序.这是Java中的错误吗?
package sorted_subsegments;
import java.util.Arrays;
public class sortTest {
public static void main(String[] args) {
int A[] = {3, 2, 1};
System.out.format("A: %s\n", Arrays.toString(A));
Arrays.sort(A, 0, 1);
System.out.format(" after sub array sort on A: %s\n", Arrays.toString(A));
System.out.println("Should be A: [2, 3, 1]");
Arrays.sort(A);
System.out.format(" whole array sort on A: %s\n", Arrays.toString(A));
}
}
Run Code Online (Sandbox Code Playgroud)