I've read in other posts, that instead of writing just System.out.println(finalPressedKey);
you should write System.out.println(Arrays.toString((finalPressedKey));
because otherwise it will just return the location where the String is saved (as far as I understood it).
public static String PressedKey[] = new String[2000];
public static String[][] finalPressedKey = {{ "", "", "", "", "", "", "", "", "", "", "", "" }}; // 12
public static String FPK3;
public static void upcounter(KeyEvent e) {
for (int x = 0; x < PressedKey.length; x++) {
if (PressedKey[x] != null && PressedKey[x + counter] != null) {
//FPK counter is supposed to be a line, and counter is where the words are supposed to be saved
finalPressedKey[FPKcounter][counter] =
finalPressedKey[FPKcounter] + PressedKey[x + counter];
System.out.println(Arrays.toString(finalPressedKey));
}
}
Whenever I Press a Button, it should be saved in my PressedKey
Array, and finalPressedKey is supposed to contain itself, and PressedKey
(also , only the last element of the array is supposed to be printed), but instead it just prints [[Ljava.lang.String;@76f42c4b]
I also tried using Arrays.deepToString();
but it gives me the same output as with Arrays.toString();
Thanks for your help!
A String[][]
is not a 2-d array. It is an array of String[]
. The difference is subtle but important.
The method Arrays.toString()
takes an array, iterates through its elements, calls toString()
on all of them, and adds a prefix, suffix, and delimiters. Since you give it a String[][]
(an array of String[]
), it will do the following:
String[]
) toString()
on each element - giving the default toString()
value of an array - ie its memory address (not really but for this purpose it doesn't matter) Luckily for you, there is an easier way - just use Arrays.deepToString()
. This behaves as you would expect.
I did not understand the whole code, but following statement is very suspicious:
finalPressedKey[FPKcounter][counter] =
finalPressedKey[FPKcounter] + PressedKey[x + counter];
since it is adding an array ( finalPressedKey[...]
) to a string ( PressedKey[...]
), which will result in that strange text - the standard textual representation of an array (returned by toString
). (from a mathematical point of view , it's strange to have 2 indexes )2D_ before the assignment and only one on the right side (1D) for same matrix)
I'm not sure, since we cannot see what counter
is, but I believe you wanted something like:
finalPressedKey[FPKcounter][counter] =
finalPressedKey[FPKcounter][counter] + PressedKey[x + counter];
that is, an additional [counter]
on second line.
This can also be written as
finalPressedKey[FPKcounter][counter] += PressedKey[x + counter];
You have to print the elements of your array with
for(int i = 0; i<finalPressedKey[0].length; i++){
for(int j=0; j<finalPressedKey[1].length; j++){
System.out.println(finalPressedKey[i][j]);
}
}
if I understand it correctly.
If you only want to store lines of strings, a normal String[] is good for you
finalPressedKey[FPKcounter] += PressedKey[x + counter];
even though I wouldn't recomment doing this, no matter what you're trying to accomplish, since this will create a new String object each time a key is pressed.
Maybe ask the question differently and tell us what you're trying to do. I guess String arrays might not be the way to go.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.