循环中的Java布尔条件

时间:2015-10-16 02:18:40

标签: java arrays loops boolean

我正在尝试计算并返回两个相等长度的DNA序列不同的地方。例如,给定字符串" ATGT"和" GTGA",结果应该是数组{true,false,false,true}。我得到一个False / true的错误无法解析为变量heres到目前为止我得到了

  destroy: function() {
    if (this.isDestroyed) { return this; }

    var args = _.toArray(arguments);

    this.triggerMethod.apply(this, ['before:destroy'].concat(args));

    // mark as destroyed before doing the actual destroy, to
    // prevent infinite loops within "destroy" event handlers
    // that are trying to destroy other views
    this.isDestroyed = true;
    this.triggerMethod.apply(this, ['destroy'].concat(args));

    // unbind UI elements
    this.unbindUIElements();

    this.isRendered = false;

    // remove the view from the DOM
    this.remove();

    // Call destroy on each behavior after
    // destroying the view.
    // This unbinds event listeners
    // that behaviors have registered for.
    _.invoke(this._behaviors, 'destroy', args);

    return this;
  },

4 个答案:

答案 0 :(得分:2)

您需要在循环中迭代mutPoint.length次(而不是i次)。您想要比较i索引处的字符(而不是String s)。你需要返回数组。像,

boolean[] mutPoint = new boolean[dna1.length()];
for (int i = 0; i < mutPoint.length; i++) {
    mutPoint[i] = dna1.charAt(i) != dna2.charAt(i);
}
return mutPoint;

喜欢

char[] dna1arr = dna1.toCharArray();
char[] dna2arr = dna2.toCharArray();
boolean[] mutPoint = new boolean[dna1arr.length];
for (int i = 0; i < mutPoint.length; i++) {
    mutPoint[i] = dna1arr[i] != dna2arr[i];
}
return mutPoint;

答案 1 :(得分:0)

这是一个简短的回答:

public static boolean[] mutationPoints(String dna1, String dna2) {
          boolean [] mutPoint =  new boolean [dna1.length()]; 
          for (int i = 0 ; i < mutPoint.length ; i++){
              mutPoint[i] = dna1.charAt(i) != dna2.charAt(i);
          }
    }

答案 2 :(得分:0)

尝试以下算法:

String dna1 = "ATGT";
        String dna2 = "GTGA";

        if(dna1.length() != dna2.length()) return;

        boolean [] mutPoint =  new boolean [dna1.length()];
        for( int i = 0; i < dna1.length(); i++) {
            if( dna1.charAt(i) == dna2.charAt(i)) {
                mutPoint[i] = false;
            }
            else {
                mutPoint[i] = true;
            }
        }

        System.out.println(Arrays.toString(mutPoint));

打印:[true, false, false, true]

注意事项:

  1. 循环应该以dna的长度运行。
  2. 您需要检查两个dnas的长度是否相等。
  3. 您需要比较字符串中字符的相等性。

答案 3 :(得分:0)

我认为只有长度相等的情况下才需要比较dna序列。

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class TestJSONDeserialize {

    public static void main(String[] args){

        ObjectMapper mapper = new ObjectMapper();
        mapper.addMixIn(Data.class, NewMixin.class);

        String jsonString = "{\"date\":\"2014-02-10\",\"time\":\"16:15:00\"}";
        Data data = null;
        try {
            data = mapper.readValue(jsonString, Data.class);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(data != null){
            System.out.println(" Data " + data.getDate());
            System.out.println(" Data " + data.getTime());
        }   
    }


}



import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

public class CustomDateDeserializer extends JsonDeserializer<XMLGregorianCalendar> {

    private static SimpleDateFormat formatter = 
      new SimpleDateFormat("yyyy-MM-dd");


    @Override
    public XMLGregorianCalendar deserialize(JsonParser jsonparser, DeserializationContext arg1) throws IOException, JsonProcessingException {
        // TODO Auto-generated method stub
        String date = jsonparser.getText();
        try {

            GregorianCalendar c = new GregorianCalendar();
            c.setTime(formatter.parse(date));
            XMLGregorianCalendar date2 = null;
            try {
                date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
            } catch (DatatypeConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return date2;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}




import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

public class CustomTimeDeserializer extends JsonDeserializer<XMLGregorianCalendar> {

    private static SimpleDateFormat formatter = 
      new SimpleDateFormat("hh:mm:ss");


    @Override
    public XMLGregorianCalendar deserialize(JsonParser jsonparser, DeserializationContext arg1) throws IOException, JsonProcessingException {
        // TODO Auto-generated method stub
        String date = jsonparser.getText();
        try {

            GregorianCalendar c = new GregorianCalendar();
            c.setTime(formatter.parse(date));
            XMLGregorianCalendar date2 = null;
            try {
                date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
            } catch (DatatypeConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return date2;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}



import javax.xml.datatype.XMLGregorianCalendar;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

abstract class NewMixin {

    @JsonDeserialize(using = CustomDateDeserializer.class)
    public abstract void setDate(XMLGregorianCalendar date);

    @JsonDeserialize(using = CustomTimeDeserializer.class)
    public abstract void setTime(XMLGregorianCalendar time);
}

输出:

[true,false,true,true]