使用类声明填充带有int和字符串的数组

时间:2017-09-19 18:40:02

标签: java arrays class

所以我不确定我在这里找不到基于我最初声明的已建立阵列打印数组的步骤。我知道java有其他方法可以打印数组但是我试图让步骤更长(而不是使用Java快捷方式),因此将相同的方法应用于其他语言会更容易。如果有人能看到我所拥有的东西并告诉我我错过了什么,我会很感激。

更新:我感到非常愚蠢,因为这是一个简单的解决方案,但现在我正处理另一个问题。我需要数组接受字符串和整数,所以我创建了一个公共类型字和数字的类,但我不确定如何正确填充数组。我知道我没有数字设置填充数组,但如果有人可以帮我找出一个我可以自己做另一个。

   package employeetotalhours;

 import java.util.ArrayList;

 public class employeetotalhours {  // main class declaration

public static class Table{          // sub class declaration
    public int number;      // establish input field values for ints
    public String word;     // establish input field values for strings
    Object array[];
}

public static void main(String[] args){ // main method  

Table[] table = new Table[9];

Table word = null;

table[0] = word;

ArrayList<Table> Table = new ArrayList<Table>();

Table.add(new Table());

 //         {{"Employee ","Su","M","T","W","T","F","Sa","Total"}, 
 //         {"EmpID 0       ","4 ","2","5","3","4","5","8 ", "0"}, 
 //         {"EmpID 1       ","1 ","7","8","4","3","3","4 ", "0"}, 
 //         {"EmpID 2       ","2 ","3","3","4","3","3","2 ", "0"}, 
 //         {"EmpID 3       ","3 ","3","3","7","3","4","1 ", "0"}, 
 //         {"EmpID 4       ","1 ","3","2","4","3","6","3 ", "0"}, 
 //         {"EmpID 5       ","5 ","3","4","4","6","3","4 ", "0"}, 
 //         {"EmpID 6       ","8 ","3","7","4","8","3","8 ", "0"}, 
 //         {"EmpID 7       ","2 ","6","3","5","9","2","7 ", "0"}};
    // all array data, multiple lines can be used as long as code is not closed with ;

    int rows = 9; // establish array row size

    int columns = 9; // establish array column size

    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            System.out.print(table[i] + " ");
        }
            System.out.println();
    }
}
}

2 个答案:

答案 0 :(得分:4)

在打印所有值的循环之前,您有这个循环:

for(int i = 0; i < rows; i++) // loop to cycle through cells in array table
    for(int j = 0; j < columns; j++)
        array[i][j] = 0;

在此循环中,您将数组的每个元素设置为0 所以当你打印元素时,一切都是0

答案 1 :(得分:1)

将所有数组元素写入零,这就是将值重新初始化为0的原因。

$("#open1").click(function() {
  $("#win1").show().kendoWindow({
    width: "500px",
    height: "500px",
    modal: true,
    title: "Window 1"
  });
});

//debugger;

$("#open2").click(function() {
  $("#win2").show().kendoWindow({
    width: "500px",
    height: "500px",
    modal: true,
    title: "Window 2"
  }).data("kendoWindow").center();
});

$("#close2").click(function() {
  $("#win2").data("kendoWindow").close();
});


$(document).ready(function() {
  $('.pai-del-menu').hide();
  var cnt = 0;
  var grid = $("#grid").kendoGrid({
    dataSource: {
      data: [{
        FileName: '1 Download CSV (via btoa)',
        ThreeDots: 'ThreeDots1'
      }, {
        FileName: '2 Download CSV (via btoa)',
        ThreeDots: 'ThreeDots2'
      }, {
        FileName: '3 Download CSV (via btoa)',
        ThreeDots: 'ThreeDots3'
      }, {
        FileName: '4 Download CSV (via btoa)',
        ThreeDots: 'ThreeDots4'
      }, {
        FileName: '5 Download CSV (via btoa)',
        ThreeDots: 'ThreeDots4'
      }],
      schema: {
        model: {
          fields: {
            FileName: {
              type: "string"
            },
            ThreeDots: {
              type: "string"
            }
          }
        }
      },
      sort: {
        field: "FileName",
        dir: "asc"
      },
      pageSize: 10
    },
    height: 500,
    scrollable: true,
    sortable: true,
    selectable: true,
    filterable: true,
    pageable: true,
    columns: [{
      field: "FileName",
      title: "File Name",
      //template: "<a id=\"sportsDataPlayer\" download=\"download.csv\" type=\"text\/csv\">#= FileName.substring(FileName.lastIndexOf('/')+1) #</a>"      
      template: function csvDownload() {
        //return "testing" + that.FinancialDocuments.ImageType(model);
        var htmlString = "<a id=\'sportsDataPlayer\' download=\'download.csv\' type=\'text\/csv\'>#= FileName.substring(FileName.lastIndexOf('/')+1) #</a>"
        var csv = "a,b,c\n1,2,3\n";
        var data = new Blob([csv]);
        var a2 = document.getElementById("sportsDataPlayer");
        alert("I am here");
        a2.href = URL.createObjectURL(data);

        }, 
      field: "ThreeDots",
      title: "Last Name",
      template: "<span class='deleteRow'>ThreeDots</span>"
    }]
  }).data("kendoGrid");


  //var csv = "a,b,c\n1,2,3\n";
  //var data = new Blob([csv]);
  //debugger;
  //var a2 = document.getElementById("sportsDataPlayer");
  //alert("I am here");
  //a2.href = URL.createObjectURL(data);


  // grid.tbody.parents(".k-grid-content").eq(0).kendoScroller({ useOnDesktop: false });

  // Start by hiding the confirmation
  $('.testingConfirmation').hide();

  // Create a persistent value for the window
  var testingWindowVal;

  // Listen to save button click
  $("#save").click(function() {
    // Set the persistent value
    testingWindowVal = $("#fname").val();

    // Show the dialog
    if (testingWindowVal) {
      $('.testingConfirmation').show();
    }
  });

  // Listen to yes from the confirmation
  $('#deleteDocumentYes').click(function() {
    // Update necessary things
    grid.dataSource.add({
      "FileName": testingWindowVal,
      "ThreeDots": "Last Name"
    });
    $("#fname").val('');

    // Remove the value
    testingWindowVal = null;

    // Hide the dialog
    $('.testingConfirmation').hide();
  });

  // Listen to no from the confirmation
  $('#deleteDocumentNo').click(function() {
    // Remove the value
    testingWindowVal = null;

    // Reset the #fname
    $("#fname").val('');

    // Hide the dialog
    $('.testingConfirmation').hide();
  });
});