简体   繁体   English

如何访问Java对象的某些部分?

[英]How do I access parts of my Java object?

I'm almost positive this question has been asked before, as I'm sure I'm not the first person to have this problem, but I don't know the right search words to get my answer, so if you could point me in the right direction, I'd really appreciate it. 我几乎肯定以前曾问过这个问题,因为我确定我不是第一个遇到此问题的人,但是我不知道正确的搜索词来获得我的答案,所以如果您能指出我的话在正确的方向上,我将非常感激。

I have an Object in a Java class, that when I mouse over, says: 我在Java类中有一个对象,当我将鼠标悬停在上面时,它说:

variableName = Object[6]
  >[0] = "words in here"
  >[1] = "a few more words"
  >[2] = float
  >[3] = float
  >[4] = integer
  >[5] = integer

I want to print out "words in here" and get the value of the integer in [5]. 我想打印出“这里的单词”并在[5]中获取整数的值。 How do I do this? 我该怎么做呢? I've tried a couple of things like 我已经尝试了一些类似的方法

String whatsNeeded = variableName[0];
String whatToGet = variableName.toString();

But neither of those get what I want. 但是这些都没有我想要的。 In fact, variableName[0] gives me an error, and variableName.toString() prints [Ljava.lang.Object;@ blahblah 实际上,variableName [0]给我一个错误,variableName.toString()打印[Ljava.lang.Object; @ blahblah

Any tips on how to get the fields I want? 关于如何获取所需字段的任何提示? Thanks!! 谢谢!!

String whatsNeeded = variableName[0];

Gives you an error because you have an Object[] type, so variableName[0] has type Object , not String . 由于您具有Object[]类型而给您一个错误,因此variableName[0]类型为Object ,而不是String You must downcast it: String whatsNeeded = (String)variableName[0] . 您必须向下转换: String whatsNeeded = (String)variableName[0]

Regarding variableName.toString() , toString on an array data type doesn't work as you intend and just prints the reference in memory. 关于variableName.toString() ,数组数据类型上的toString不能按预期工作,仅在内存中打印引用。 You should use Arrays.toString(variableName) to get a proper printed value. 您应该使用Arrays.toString(variableName)获得正确的打印值。

Looking at your comment it looks like you can't access items of variableName through [] operator. 查看您的评论,看来您无法通过[]运算符访问variableName项。 This happens because you probably assigned it to an Object type variable, eg: 发生这种情况是因为您可能将其分配给了Object类型变量,例如:

Object[] array = new Object[6];
Object object = array;

array[0] // valid
object[0] // invalid, it is an array at runtime but not at compile time

If that's the case you must first cast it to an array: 如果是这种情况,则必须首先将其转换为数组:

Object asArray = (Object[])variableName;
String whatsNeeded = (String)asArray[0];

尝试投射,例如:

 String tmp=(String)varName[0];

When you do this: 执行此操作时:

variableName.toString()

you are invoking the toString method so you need to know how it's working or you can override the method. 您正在调用toString方法,因此您需要了解它的工作方式,或者可以重写该方法。

Now, if you want to print one element of your array, in this case the first element, you need to know the type of the object, if your program is dynamic, then you need a set of methods or only "if" sentences to verify each element of your array and finally you must do a casting on the object, for example: 现在,如果要打印数组的一个元素(在本例中为第一个元素),则需要知道对象的类型,如果程序是动态的,则需要一组方法或仅使用“ if”语句验证数组中的每个元素,最后必须对对象进行强制转换,例如:

if (variableName[0] instanceof String) {
((String)variableName[0]).toString();
}

I hope this information helps you. 希望这些信息对您有所帮助。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM