Hello everyone. I'm learning java and i'm really new at this so i know this is probably too simple for you but if you can show my mistake i will be grateful. I made 2 classes. One is the main and the other creates an int array. Everything seems fine when i first start the program but when i try to add a value to the first index of array "if" becomes false and skip to the "else" part so i get "Out of limit!" error that i wrote. Also the show list method is wrong too i guess but i can't find my mistake. Here's my code.
public class MaximumBulma { int[] List; int i; int x=0; int Limit; public MaximumBulma(int Capacity){ List = new int[Capacity]; i=0; Capacity=Limit; } public void AddToList(int eklenen) { if (i + 1< Limit){ List[i]=eklenen; i++; } else{ System.err.println("Out of limit!"); } } public void ShowList(){ while (x+1<Limit){ System.out.println(List[x]); x++; } } }
and the main
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Capacity?"); int GirilenLimit=scan.nextInt(); MaximumBulma Max = new MaximumBulma(GirilenLimit); System.out.println("Value to add?"); int GirilenDeger=scan.nextInt(); Max.AddToList(GirilenDeger); Max.ShowList(); } }
thank you for your help!