Fragen zum Array.length

Status
Nicht offen für weitere Antworten.

pavelle

Mitglied
Hallo,


habe folgenden Code:

Java:
public class aufgabe11 {


    public static void main(String[] args) {


            String[][] Wasserkasten_Glas ={{"Glas","PET","Glas","leer"},
                                       {"Glas","Glas","PET","Glas"},
                                       {"PET","PET","leer","Glas"}};


            for(int y=0; y<Wasserkasten_Glas.length; y++)
            {
                for(int x=0; x<Wasserkasten_Glas.length; x++)
                {
                System.out.print(Wasserkasten_Glas[y][x]+" ");
                }
                System.out.println("");
            }
    }

}


Dabei handelt es sich um ein zwei dimensionales 4x3 Array.
Der oben stehende Code, gibt von der ersten 4er Zeile nur die ersten 3 Strings aus, da das Wassertkasten_Glas.length aus der äusseren For Schleife eben auf 3 limitiert wurde.
Wie kann ich nun beide length Angaben unabhängig von einander machen?

gruß
 
Indem du dich innerhalb der äußeren Schleife
auf die Länge des aktuell durchlaufenen Elements
durch Angabe des Indizes beziehst!
 
schneller als pavelle selber die Lösung schreiben konnte
(vielleicht bin ich jetzt mit dem Hinweis wieder schneller als pavelle 😉 )
 
Kannste auch einfacher haben.
Java:
public class aufgabe11
{
	public static void main(String[] args)
	{
		String[][] Wasserkasten_Glas = { { "Glas", "PET", "Glas", "leer" },
				{ "Glas", "Glas", "PET", "Glas" },
				{ "PET", "PET", "leer", "Glas" } };

		for (String[] zeile : Wasserkasten_Glas)
		{
			for (String spalte : zeile)
			{
				System.out.print(spalte + " ");
			}
			System.out.println();
		}
	}
}
 
So, geschafft, habe einfach mal wircklich den ganzen Test gedebuggt.
Danke für eure Hilfe.
Hier noch der Code:

Java:
package u4a1;

import java.util.EmptyStackException;

/**
 * Dynamically growing stack.
 */
public class Stack {
	private int[] buffer;
	private int size;

	/**
	 * Creates a new stack
	 * 
	 * @param capacity the initial capacity of the stack
	 */
	
	public Stack(int capacity) //Constructor
	{
		
		buffer = new int [capacity];
		size = 0;
	}
	

    /**
	 * Converts stack to a string representation.
	 * 
	 * @return A ", "-separated list of the numbers, enclosed in square brackets. Bottom numbers come first. 
	 */
	public String toString() //convert Stack toString
	{
		String stackString = "[";
		for (int i = 0; i<size;i++)
		{
			if((size-1) > i)
			{
				stackString = stackString + buffer[i]  + ", "; 
			}
			if ((size-1) == i)
			{
				stackString = stackString + buffer[i];
			}
		}
		stackString = stackString + "]";
		return stackString;
	}
	
	/**
	 * Doubles the capacity of the stack.
	 * 
	 * Copies all objects to a new buffer of doubled size.
	 */
	private void grow()
	{
		int [] copyBuffer = new int [2*buffer.length];
		for (int i = 0; i<size;i++)
		{
			copyBuffer[i] = buffer[i];
		}
		buffer = copyBuffer;		
	}

	/**
	 * Pushes a number onto the top of this stack.
	 * 
	 * Grows the stack if necessary.
	 * 
	 * @param number the number to be pushed onto this stack. 
	 */
	public void push(int number)
	{
		if (size >= buffer.length)
		{
			grow();
			buffer[size] = number;
			size++;
		}
		else 
		{
			buffer[size] = number;
			size++;
			
		}
	}
	
	/**
	 * Removes the number at the top of this stack and returns it as the value of this function. 
	 * 
	 * @return The number at the top of this stack
	 * @throws EmptyStackException if this stack is empty
	 */
	public int pop() throws EmptyStackException 
	{
		int topNumber;
		if (!empty())
		{
			size--;
			topNumber = buffer[size];
			
		}
		else 
		{
			throw new EmptyStackException();
		}
		
		return topNumber;
	}


	/**
	 * Looks at the number at the top of this stack without removing it from the stack. 
	 * 
	 * @return the number at the top of this stack
	 * @throws EmptyStackException if this stack is empty
	 */
	public int peek() throws EmptyStackException
	{
		int topNumber;
		if (!empty())
		{
			topNumber = buffer[size-1];
			
		}
		else 
		{
			throw new EmptyStackException();
		}
		
		return topNumber;
		
	}

	/**
	 * Tests if this stack is empty. 
	 * 
	 * @return true if and only if this stack contains no items; false otherwise.
	 */
	public boolean empty() 
    {
		if (size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * Get the size of this stack.
	 * 
	 * @return the current number of items on this stack
	 */
	public int size() 
    {
		return size;
	}
	
	/**
	 * Get the capacity of this stack.
	 *    
	 * @return the maximum number of items this stack can hold without having to grow
	 */
	public int capacity() 
    {
		return buffer.length;
	}
}
 
Sehr schön gemacht, auch wenn ich eine Kleinigkeit anmerken würde 😀
Java:
if (size >= buffer.length)
        {
            grow();
            buffer[size] = number;
            size++;
        }
        else 
        {
            buffer[size] = number;
            size++;
            
        }
Das geht auch kürzer...
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben