Hello, fellow programmers. Today I encountered a problem getting the 2d array length. By this I mean How many x values are filled, that all the x's sub values are filled {y}. ex
x-> 1, 2, 3 ,4, 5, 6
y 1, 1, 1
| 2, 2, 2
3, 3, 3

The code below always returns the arrayLength = 1, which with an already filled array it is supposed to be 5. This function is supposed to give me the last filled x value which in function getArrayLength() = c;

public static final int X_DIMENSION_SIZE = 4;
	public static String[] lineByLine = new String[500];
	public static String[][] lineByObject = new String[X_DIMENSION_SIZE][500];
        public static int arrayLength = 1;
        
        public static void testlength()
        {
            getArrayLength();
            System.out.println(arrayLength);
        }
        public static void getArrayLength()
        {
            for(int c =1; c < 500; c++)
            {
                if(lineByObject[0][c] == null && lineByObject[1][c] == null && lineByObject[2][c] == null && lineByObject[3][c] == null)
                {
                    arrayLength = c -1;
                    break;
                }
            }
        }

Hope you can help me. And for extra information, I need this because I am writing info to a file line by line and storing them using delimiters and if I don't have the length of the array, after writing the filled values -> 123|Hello|Hi become -> ||| which would give me problems if I want to read the file.

Recommended Answers

All 3 Replies

The code below always returns the arrayLength = 1, which with an already filled array it is supposed to be 5.

Can you post code with the array filled as desired that compiles, executes and shows the problem?

Try debugging your code by adding a println to show the value of arrayLength every time its value is changed.
You might change the array size to 50 vs 500 for testing.

In Java, 2D arrays are really arrays of arrays with possibly different lengths (there are no guarantees that in 2D arrays that the 2nd dimension arrays all be the same length)

You can get the length of any 2nd dimension array as z[n].length where 0 <= n < z.length.

If you're treating your 2D array as a matrix, you can simply get z.length and z[0].length, but note that you might be making an assumption that for each array in the 2nd dimension that the length is the same (for some programs this might be a reasonable assumption)

try this for clarification:

public class testdimarray
{
    static int[][] arr=new int[5][6];
   public static void main (String[] args)
	{
	System.out.println(arr.length);//this will print the first size
	System.out.println(arr[0].length);//print 2nd size
	//System.out.println(arr[1].length); //this will give you the same as above-6
	}
}

Found the Problem I didn't initialize the read function.. what an idiot am I, but for the people who have the same problem and wish to use this code, IT WORKS at least for me.! Thank you DaniWeb

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.