-1

Everytime I run the code I get an Index 12 out of bounds for length 12. I'm not sure what is the problem. Here is the code:

enter code here: int getDayNumber(int day,int month,int year) {
    if(this.leapYear(year)) {
        int months[] = {31,29,31,30,31,30,31,31,30,31,30,31};
        int cnt = 0 ;
        for(int i=0;i<month-1;i++) {
            cnt += months[i];
            System.out.println(cnt + "   " + i);
        }
        return cnt + day ; // count = cnt

   

enter image description here

Saitou9
  • 9
  • 1

3 Answers3

0

The (month - 1) that you've used inside the loop points to the month passed in the parameter in the function getDayNumber. Consider using .length function to calculate the length of the array. Try using for loop something like:

for(int i = 0 ; i < months.length ; i++)

Your concept of how Array works is pretty clear! Just use months.length and you're good to go. Tell me how this works out for you!

0
    package projectlearn1;

public class ArrayQueue {
    private int[] mArray;
    private int mCount;

    public ArrayQueue(int sz) {
        mArray=new int[sz];
        mCount=0;
    }

    public void add(int val){
        mArray[mCount++]=val;
    }

    public int front(){
        return mArray[0];
    }

    public int pop(){
        int ret=mArray[0];
        mCount--;
        for (int i = 0; i < mCount; i++) {
            mArray[i-1]=mArray[i];
        }
        return ret;
    }
    public int size(){
        return mCount;
    }

    public boolean isEmpty(){
        return size()==0;
    }

    public static void main(String[] args) {
        int tmp=0;
        ArrayQueue astack=new ArrayQueue(12);
        astack.add(10);
        astack.add(20);
        astack.add(30);

        tmp=astack.pop();
        System.out.printf("tmp=%d\n",tmp);

        tmp=astack.front();
        System.out.printf("tmp=%d\n",tmp);

        astack.add(40);

        System.out.printf("isEmpty()=%b\n",astack.isEmpty());
        System.out.printf("size()=%d\n",astack.size());
        while (!astack.isEmpty()){
            System.out.printf("size()=%d\n",astack.pop());
        }
    }
}
0
import java.util.*;
public class BinaryTreessssB {

    static class Node {
        int data;
        Node left;
        Node right;

        Node(int data) {
            this.data = data;
            this.left = null;
            this.right = null;
        }

    }
    static class BinaryTree{
        static int idx = -1;
        public static Node buildTree(int nodes[]){
            idx++;
            if (nodes[idx] == -1){
                return null;
            }
            Node newNode = new Node(nodes[idx]);
            newNode.left = buildTree(nodes);
            newNode.right = buildTree(nodes);


            return newNode;
        }

        public static void preorder(Node root){
            if (root == null){

            }
            System.out.println(root.data+" ");
            preorder(root.left);
            preorder(root.right);
        }

    }
    public static void main(String[] args) {
        int nodes[] = {1, 2, 4, -1, 5, -1, -1, 3, -1, 6, -1, -1};
        BinaryTree tree = new BinaryTree();
        Node root = tree.buildTree(nodes);
        tree.preorder(root);
    }
}

enter image description here

starball
  • 20,030
  • 7
  • 43
  • 238
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 19 '23 at 06:29