85

I have a string array selectCancel with setter and getter methods, which is a checkbox in my form. I am trying to get the checked values and I am getting the above result when I print.

I tried the Arrays.toString() method but it still prints the same.

I then did the following:

String checked = Arrays.toString(Employee.getSelectCancel());

I also tried with the Arrays.asList() and Arrays.copyOf()

So, how do I read this string?

1
  • 13
    An array with getter/setter which is a checkbox? #lost Mar 26, 2012 at 8:01

6 Answers 6

112

The method works if you provide an array. The output of

String[] helloWorld = {"Hello", "World"};
System.out.println(helloWorld);
System.out.println(Arrays.toString(helloWorld));

is

[Ljava.lang.String;@45a877
[Hello, World]

(the number after @ is almost always different)

Please tell us the return type of Employee.getSelectCancel()

31

Ljava.lang.String;@ is returned where you used string arrays as strings. Employee.getSelectCancel() does not seem to return a String[]

1
  • Once the string array is sorted with Arrays.sort(a); convert it into strings by using Arrays.toString(a) and then print it. Whole code will look like this: import java.util.Arrays; class Playground { public static void main(String[ ] args) { System.out.println("Hello Java"); String [] a = { "ew" , "ds" , "as" }; Arrays.sort(a); System.out.println (Arrays.toString(a)); } } Jun 21, 2019 at 6:52
13

According to the Java Virtual Machine Specification (Java SE 8), JVM §4.3.2. Field Descriptors:

FieldType term | Type      | Interpretation
-------------- | --------- | --------------
L ClassName ;  | reference | an instance of class ClassName
[              | reference | one array dimension
...            | ...       | ...

the expression [Ljava.lang.String;@45a877 means this is an array ( [ ) of class java.lang.String ( Ljava.lang.String; ). And @45a877 is the address where the String object is stored in memory.

12

[ stands for single dimension array
Ljava.lang.String stands for the string class (L followed by class/interface name)

Few Examples:

  1. Class.forName("[D") -> Array of primitive double
  2. Class.forName("[[Ljava.lang.String") -> Two dimensional array of strings.

List of notations:
Element Type : Notation
boolean : Z
byte : B
char : C
class or interface : Lclassname
double : D
float : F
int : I
long : J
short : S

1

I have the same trouble: I make my own methods: So if I gonna call method like this:

Show("Additional String like this:"+ MyArray);//wrong command

have error! It's must be without "Additional String like this:" just do like this:

Show(AnyArray);//right command

package j;

class J{
public static String [] AnyArray = new String[3];

public static void main(String[] args) {

AnyArray[0]="String_0";
AnyArray[1]="String_1";
AnyArray[2]="String_2";

/******************************************************/
Show(AnyArray); //right
/*****************************************************/
Show("Additional String like this"+AnyArray);//wrong
/****************************************************/
}


public static void Show(String[] MyArray)
{
for(int i=0;i<=MyArray.length-1;i++){
System.out.println("MyArray ["+i+"]: "+MyArray[i]+"");
 }
}



public static void Show(String MyString)
{
System.out.println(MyString);
 }

}
0

I also met this problem when I've made ListView for android app:

Map<String, Object> m;

for(int i=0; i < dates.length; i++){
    m = new HashMap<String, Object>();
    m.put(ATTR_DATES, dates[i]);
    m.put(ATTR_SQUATS, squats[i]);
    m.put(ATTR_BP, benchpress[i]);
    m.put(ATTR_ROW, row[i]);
    data.add(m);
}

The problem was that I've forgotten to use the [i] index inside the loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.