Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why does it fill out the entire array instead of just one index?

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why does it fill out the entire array instead of just one index?

     	private int anzahlWagen = 0;
    	static Wagen[] wagenList;
    	public static void main(String[] args){
    		wagenList = new Wagen[6];
    		for(int i = 0;i < wagenList.length;i++){
    			wagenList[i] = null;
    		}
    		addWagen(5,9);
    		for(int i = 0;i < wagenList.length;i++){
    		System.out.println(wagenList[i].getFirstClass());
    		}
    	}
     
    	public static void addWagen(int pFirst, int pSecond){
    		boolean wagenAdded = false;
    		for(int i = 0;i < wagenList.length;i++){
    			if(wagenList[i] == null){
    				if(wagenAdded == false){
    				wagenAdded = true;
    				wagenList[i] = new Wagen(pFirst, pSecond);
    				}
    			}
    		}
    		wagenAdded = false;
    	}

    It adds the value 5 to all objects within the array. The class "Wagen" consists of two private int variables, and get-methods.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why does it fill out the entire array instead of just one index?

    Can you print out the contents of the array and post it so I can see what you are asking about?
    Use the Arrays class's toString method to format the array for printing.
    System.out.println("the array= "+ java.util.Arrays.toString(theArrayName));

    Add some comments to the print out describing what is wrong and show what the desired output is.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How do we fill in an array using substring, FileReader and a txt file?
    By wholegrain in forum Java Theory & Questions
    Replies: 5
    Last Post: February 12th, 2012, 03:18 PM
  2. Constructor doesn't fill in array properly
    By Whyareall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 6th, 2010, 04:24 AM
  3. Fill & sort an array
    By yroll in forum Collections and Generics
    Replies: 2
    Last Post: April 1st, 2010, 06:00 AM
  4. Fill array concurrently
    By mamba in forum Collections and Generics
    Replies: 2
    Last Post: October 15th, 2009, 07:08 PM
  5. [SOLVED] Java program to sort arrays containing dates
    By scottyadam in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:08 PM

Tags for this Thread