String保持初始化为null

时间:2017-02-13 02:42:13

标签: java

我的字符串一直将自己初始化为null ...或者至少看起来像这样。我正在尝试为我的矩阵创建自定义toString函数。 this.matrixArray是尺寸为'm x n'的二维数组。

public String toString() {
    String stringAsMatrix = "";
    String dimensions = this.m + "," + this.n + "\n";
    stringAsMatrix += dimensions;
    String[] dataRows = new String[this.m];

    for (int row = 0; row < this.m; row++) {
        for (int column = 0; column < this.n; column++) {
            String elementString = "";
            elementString += this.matrixArray[row][column];
            if (column == this.n-1) {
                dataRows[row] += elementString + "\n";
            } else {
                dataRows[row] += elementString + ","; // Only add a comma if this isn't the last element in the row
            }
        }
        stringAsMatrix += dataRows[row];
    }
    return stringAsMatrix;
} 

这是我得到的输出,但我不明白为什么它在我的字符串之前打印'null'。尺寸是正确的(矩阵阵列确实是2x2)。值本身也是正确的(my matrix is {{1,2}, {3,4}})

2,2
null1.0,2.0
null3.0,4.0

1 个答案:

答案 0 :(得分:1)

dataRows[row]

null正在以dataRows[row] = null + elementString + "\n" 开头。所以它变成

dataRows[row] = elementString + "\n";

......这正是你得到的。相反,写

function readURL(input) {
  if (input.files && input.files[0]) {
    console.log("Reading File.");
    var reader = new FileReader();
    reader.addEventListener("load", function(e) {
      if (jQuery("#preview-gallery li").length == 3) {
        input.value = "";
        return false;
      }
      var $imgP = jQuery("<img>", {
        class: "uploaded-image icon",
        src: reader.result
      });
      var $item = jQuery("<li>", {
        class: "ui-widget-content ui-corner-all hidden"
      });
      $item.append($imgP).append("<a href='#' title='Delete this image' class='ui-icon ui-icon-trash'>Delete image</a>");
      $item.appendTo(jQuery("#preview-gallery")).show("slow");
      makeDrag($item);
      updatePreviewCount();
    });
    if (input.files[0]) {
      reader.readAsDataURL(input.files[0]);
    } else {
      console.log(" Reader: File Not found.");
    }
    input.value = "";
  }
}

function renderContent() {
  html2canvas(jQuery("#firstshirt"), {
    allowTaint: true,
    logging: true
  }).then(function(canvas) {
    jQuery("#previewImage").append(canvas);
    jQuery("#download").css("display", "inline");
    jQuery("#download").attr("href", jQuery("#previewImage")[0].children[0].toDataURL());
  });
}

function makeDrag(o) {
  o.draggable({
    helper: "original",
    revert: "invalid",
    zIndex: 999
  });
}

function makeResize(o) {
  o.resizable();
}

function addImage($item, $pos) {
  console.log("  Fade Item Out");
  $item.fadeOut();
  var $img = $item.find("img");
  $img.css("width", "80px").css("height", "80px");
  $item.remove();
  updatePreviewCount();
  console.log("  Making new Wrap");
  var $drop = jQuery("<div>", {
    class: "dragbal deleteme",
    style: "position: absolute; top: 100px; left: 100px;"
  });
  $drop.append($img);
  console.log($drop);
  console.log("  Appending to #boxes");
  $drop.appendTo(jQuery("#boxes")).fadeIn();
  $drop.draggable({
    containment: "#boxes"
  });
  makeResize($drop.find("img"));
}

function updatePreviewCount() {
  var cnt = jQuery("#preview-gallery li").length;
  jQuery(".upPreview span").html(cnt + "/3");
}

jQuery(download).ready(function() {
  // Setup
  jQuery(".file-upload-wrapper").hide();
  jQuery(".out-put-img").hide();
  jQuery('.smallimages').hide();

  makeDrag(jQuery("[id$='-gallery'] ul li"));

  jQuery("#boxes").droppable({
    accept: ".gallery > li",
    drop: function(e, ui) {
      console.log("Drop Pos:", ui.offset);
      addImage(ui.helper, ui.position);
    }
  });

  // Events
  jQuery("#btn-Preview-Image").click(function(e) {
    e.preventDefault();
    renderContent();
    jQuery("#download").removeClass("disabled");
  });

  jQuery("#download").click(function(e) {
    e.preventDefault();
    return jQuery(this).hasClass("disabled");
  });

  jQuery("#imajes45").change(function() {
    if (jQuery('#imajes45').val() == "new-upload") {
      jQuery(".file-upload-wrapper").show();
      jQuery(".file-select").hide();
    } else {
      jQuery(".file-upload-wrapper").hide();
      jQuery(".file-select").show();
    }
  });

  jQuery(".file-select").change(function() {
    jQuery(".upPreview").hide();
    jQuery("#" + jQuery(this).val() + "-gallery").show();
  })

  jQuery(".upload-img").change(function() {
    readURL(this);
  });


});
var myform = document.getElementById('myform');

if (myform != undefined) {
  myform.onsubmit = function(e) { /* do some validation on event here */ };
}



$("#wrapper").hover(function() {
    $("#boxes").css("border-color", "red");
  },
  function() {
    $("#boxes").css("border-color", "transparent");
  });

$(document).ready(function() {


      $(".container").on("dblclick", ".deleteme", function() {
        console.log('clicked delete')
        $(this).remove();
      });