簡體   English   中英

用Java創建模式數組

[英]Creating a pattern array in Java

這可能是一個非常簡單的問題,但是我正在嘗試創建一系列模式,並且存在一些問題。 我所做的如下:

Pattern [] aminos = null;
aminos [0] = Pattern.compile("(gct)|(gcc)|(gca)|(gcg)");
aminos [1] = Pattern.compile("(tgt) | (tgc)");
aminos [2] = Pattern.compile("(gat) | (gac)");

在嘗試運行它之前,沒有語法錯誤或其他任何內容,但是當我嘗試運行它時,它在第二行中斷,顯示“空指針訪問:可變氨基酸只能在此位置為空”。 那我該如何創建一個Pattern數組呢? 當我忽略指定null時,出現了一個錯誤,要求我初始化數組,所以我不確定現在要做什么。

我想我可以將所有的正則表達式模式存儲在字符串數組中,然后編寫一個小的函數以根據需要形成模式,但是如果我只可以制作一個模式數組,則將更加方便。

感謝您的閱讀!

這是一種簡單的方法:

Pattern[] aminos = {
    Pattern.compile("(gct)|(gcc)|(gca)|(gcg)"),
    Pattern.compile("(tgt) | (tgc)"),
    Pattern.compile("(gat) | (gac)")
};

或者,您可以創建大小合適的數組以以下內容開頭:

Pattern[] aminos = new Pattern[3];

但這意味着正確計數-第一個版本將自動為您提供合適大小的數組。

或者使用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)"));

您必須使用其大小來初始化數組。 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)");

如果您不知道會有多少個Pattern ,可以使用如下的ArrayList

ArrayList<Pattern> aminos = new ArrayList<Pattern>();
aminos.add(Pattern.compile("(gct)|(gcc)|(gca)|(gcg)");
...

ArrayList是Java中數組的靈活版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM