简体   繁体   English

Java更改矩阵2D中的行索引

[英]Java change row index in matrix 2D

I try to create a matrix in java that stores a data sets. 我尝试在Java中创建一个存储数据集的矩阵。 I want to construct a matrix of 2D, where the row presents the days of year , and columns presents the hours of day. 我想构造一个2D矩阵,其中行表示一年中的天数,而列表示一天中的小时数。 This is my matrix : 这是我的矩阵:

int [][] dataset = new int [366] [27] ; 

I want to make the rows indexed using MyLocalDate.hashcode() instead of int 0 , 1 ... 365 However, I want to access to my specific data into matrix for example: 我想使用MyLocalDate.hashcode()而不是int 0,1 ... 365为行建立索引,但是,例如,我想将我的特定数据访问到矩阵中:

int data  = dataset [ MyLocalDate.hashcode() ][ 10 ] ; 

Check this code out...it will show the current day number. 检查此代码...它将显示当前日期。

Eg: 20 March 2018 --> Day of the year 79. 例如:2018年3月20日->每年的第79天。

21 March 2018 --> Day of the year 80. 2018年3月21日->每年的80。

with this i hope you can get a idea how to tackle your problem. 希望借此,您可以对如何解决自己的问题有所了解。

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DayOfYear 
{
    public static void main(String args[])
    {
       Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
       int CurrentDayOfYear = localCalendar.get(Calendar.DAY_OF_YEAR);
       System.out.println("Day of Year: " + CurrentDayOfYear);
       Calendar gmtCalendar = 
       Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    }
}

edit: it seems there are far better ways to do this in java 8. check this link 编辑:在Java 8中似乎有更好的方法来执行此操作。 请检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM