[英]Creating a pattern array in Java
This is probably a very simple problem but I'm trying to create an array of patterns and there are some issues. 这可能是一个非常简单的问题,但是我正在尝试创建一系列模式,并且存在一些问题。 What I've done is below:
我所做的如下:
Pattern [] aminos = null;
aminos [0] = Pattern.compile("(gct)|(gcc)|(gca)|(gcg)");
aminos [1] = Pattern.compile("(tgt) | (tgc)");
aminos [2] = Pattern.compile("(gat) | (gac)");
There are no syntax errors or anything before I try to run it, but when I try to run it breaks at the 2nd line saying "Null pointer access: the variable aminos can only be null at this location". 在尝试运行它之前,没有语法错误或其他任何内容,但是当我尝试运行它时,它在第二行中断,显示“空指针访问:可变氨基酸只能在此位置为空”。 How do I create a Pattern array then?
那我该如何创建一个Pattern数组呢? When I neglected to specify null an error appeared asking me to initialise the array, so I'm unsure what to do now.
当我忽略指定null时,出现了一个错误,要求我初始化数组,所以我不确定现在要做什么。
I guess I could store all the regex patterns in a String Array and then write a small function to form the patterns as needed but it would be more convenient if I could just make a Pattern array. 我想我可以将所有的正则表达式模式存储在字符串数组中,然后编写一个小的函数以根据需要形成模式,但是如果我只可以制作一个模式数组,则将更加方便。
Thanks for reading guys! 感谢您的阅读!
Here's one simple approach: 这是一种简单的方法:
Pattern[] aminos = {
Pattern.compile("(gct)|(gcc)|(gca)|(gcg)"),
Pattern.compile("(tgt) | (tgc)"),
Pattern.compile("(gat) | (gac)")
};
Alternatively, you could create an array of the right size to start with: 或者,您可以创建大小合适的数组以以下内容开头:
Pattern[] aminos = new Pattern[3];
That means getting the counting right though - the first version will automatically give you an array of the right size. 但这意味着正确计数-第一个版本将自动为您提供合适大小的数组。
Or use a List<Pattern>
instead (the collection classes are generally more pleasant to work with than arrays): 或者使用
List<Pattern>
代替(集合类通常比数组更易于使用):
List<Pattern> aminos = new ArrayList<Pattern>();
aminos.add(Pattern.compile("(gct)|(gcc)|(gca)|(gcg)"));
aminos.add(Pattern.compile("(tgt) | (tgc)"));
aminos.add(Pattern.compile("(gat) | (gac)"));
You have to initialize the array with the size it will be. 您必须使用其大小来初始化数组。 Java doesn't have flexible arrays.
Java没有灵活的数组。
Pattern[] aminos = new Pattern[3];
aminos [0] = Pattern.compile("(gct)|(gcc)|(gca)|(gcg)");
aminos [1] = Pattern.compile("(tgt) | (tgc)");
aminos [2] = Pattern.compile("(gat) | (gac)");
If you don't know how many Pattern
s you will have, you can use an ArrayList
like this: 如果您不知道会有多少个
Pattern
,可以使用如下的ArrayList
:
ArrayList<Pattern> aminos = new ArrayList<Pattern>();
aminos.add(Pattern.compile("(gct)|(gcc)|(gca)|(gcg)");
...
ArrayList
s are the flexible version of an array in Java. ArrayList
是Java中数组的灵活版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.