Java 二维数组元素上的引用
可以用java创建这样的引用吗?所以我可以保存一个数组元素( {“Marc”,“Empten”} )从阵列中 所以我只能用一个人变量来表示“Marc Empten”?Java 二维数组元素上的引用,java,arrays,reference,Java,Arrays,Reference,可以用java创建这样的引用吗?所以我可以保存一个数组元素( {“Marc”,“Empten”} )从阵列中 所以我只能用一个人变量来表示“Marc Empten”? 我不知道如何实现这一点。甚至有可能吗?你必须这样写: public class ReferenceTest { public static void main(String[] args) { String[][] names = {{"George","Hampton"},{"Marc", "Empte
我不知道如何实现这一点。甚至有可能吗?你必须这样写:
public class ReferenceTest {
public static void main(String[] args) {
String[][] names = {{"George","Hampton"},{"Marc", "Empten"},{"Levin", "Lian"}};
Object oneperson; //Reference to one object of the 2D Array
oneperson = names[1];
//Output should be Marc Empten
System.out.println(oneperson.toString()); //[Ljava.lang.String;@11890d
}
}
final String[][] names =
{ { "George", "Hampton" }, { "Marc", "Empten" }, { "Levin", "Lian" }, { "John", "James", "Rambo" } };
for (final String[] name : names) {
for (final String partial : name) {
System.out.print(partial);
System.out.print(" ");
}
System.out.println();
}
只有确定内部数组始终包含2个元素时,这才有效。如果不是,则必须使用循环打印姓名,例如(请注意,姓氏有3个部分):
你必须这样写:
public class ReferenceTest {
public static void main(String[] args) {
String[][] names = {{"George","Hampton"},{"Marc", "Empten"},{"Levin", "Lian"}};
Object oneperson; //Reference to one object of the 2D Array
oneperson = names[1];
//Output should be Marc Empten
System.out.println(oneperson.toString()); //[Ljava.lang.String;@11890d
}
}
final String[][] names =
{ { "George", "Hampton" }, { "Marc", "Empten" }, { "Levin", "Lian" }, { "John", "James", "Rambo" } };
for (final String[] name : names) {
for (final String partial : name) {
System.out.print(partial);
System.out.print(" ");
}
System.out.println();
}
只有确定内部数组始终包含2个元素时,这才有效。如果不是,则必须使用循环打印姓名,例如(请注意,姓氏有3个部分):
这样做:
public class ReferenceTest {
public static void main(String[] args) {
String[][] names = {{"George","Hampton"},{"Marc", "Empten"},{"Levin", "Lian"}};
Object oneperson; //Reference to one object of the 2D Array
oneperson = names[1];
//Output should be Marc Empten
System.out.println(oneperson.toString()); //[Ljava.lang.String;@11890d
}
}
final String[][] names =
{ { "George", "Hampton" }, { "Marc", "Empten" }, { "Levin", "Lian" }, { "John", "James", "Rambo" } };
for (final String[] name : names) {
for (final String partial : name) {
System.out.print(partial);
System.out.print(" ");
}
System.out.println();
}
这样做:
public class ReferenceTest {
public static void main(String[] args) {
String[][] names = {{"George","Hampton"},{"Marc", "Empten"},{"Levin", "Lian"}};
Object oneperson; //Reference to one object of the 2D Array
oneperson = names[1];
//Output should be Marc Empten
System.out.println(oneperson.toString()); //[Ljava.lang.String;@11890d
}
}
final String[][] names =
{ { "George", "Hampton" }, { "Marc", "Empten" }, { "Levin", "Lian" }, { "John", "James", "Rambo" } };
for (final String[] name : names) {
for (final String partial : name) {
System.out.print(partial);
System.out.print(" ");
}
System.out.println();
}
如果只想打印数组,请使用
Arrays.toString()
方法。但更好的方法是创建一个包含这两部分的类名。然后使用数组列表存储名称。像
System.out.println(Arrays.toString((Object[])oneperson));
类名{
私有字符串名;
私有字符串lastName;
公共名称(字符串名、字符串名){
this.firstName=firstName;
this.lastName=lastName;
}
@凌驾
公共字符串toString(){
返回“{”+firstName+”,“+lastName”
+ "}";
}
//接球手和接球手
}
公共类样本{
公共静态void main(字符串[]args)引发JsonProcessingException{
列表名称=新的ArrayList(Arrays.asList(新名称[]{新名称(“乔治”、“汉普顿”)、新名称(“马克”、“清空”)、新名称(“莱文”、“丽安”)});
System.out.println(names.get(1));//Prints{Marc,Empten}
}
}
如果只想打印数组,请使用Arrays.toString()
方法。但更好的方法是创建一个包含这两部分的类名。然后使用数组列表存储名称。像
System.out.println(Arrays.toString((Object[])oneperson));
类名{
私有字符串名;
私有字符串lastName;
公共名称(字符串名、字符串名){
this.firstName=firstName;
this.lastName=lastName;
}
@凌驾
公共字符串toString(){
返回“{”+firstName+”,“+lastName”
+ "}";
}
//接球手和接球手
}
公共类样本{
公共静态void main(字符串[]args)引发JsonProcessingException{
列表名称=新的ArrayList(Arrays.asList(新名称[]{新名称(“乔治”、“汉普顿”)、新名称(“马克”、“清空”)、新名称(“莱文”、“丽安”)});
System.out.println(names.get(1));//Prints{Marc,Empten}
}
}
这不起作用,因为oneperson
是对象,而不是数组。此外,它将打印[Marc,Empten]
如果它正在工作。使用强制转换这是可能的:将强制转换添加到数组这不起作用,因为oneperson
是对象,而不是数组。此外,如果它正在工作,它将打印[Marc,Empten]
。使用强制转换这是可能的:将强制转换添加到数组,因此我必须始终使用名称。get(number)我想用一个像费尔巴哈那样的“对象”,这几乎和名称[number]
一样。列表的好处是在大小上没有限制。列表基本上是一个在封面下的数组。所以它和费尔巴哈数很相似,只是当你添加物品时,它会增长。我不完全明白你说的“我想用一个物体”是什么意思。你的意思是你需要把它作为一个数组来做吗?不,我想要一个引用对象,比如evry,其他变量,用来解释数组或arraylist的一个元素…是的。这就是索引的作用。它返回索引1处的元素。这相当于Name oneperson=names.get(1)
或者你可以喜欢objectoneperson=names.get(1)代码>。所以我必须始终使用名称。get(number)我想使用像feuerball那样的“对象”。这几乎与名称[number]
相同。列表的好处是在大小上没有限制。列表基本上是一个在封面下的数组。所以它和费尔巴哈数很相似,只是当你添加物品时,它会增长。我不完全明白你说的“我想用一个物体”是什么意思。你的意思是你需要把它作为一个数组来做吗?不,我想要一个引用对象,比如evry,其他变量,用来解释数组或arraylist的一个元素…是的。这就是索引的作用。它返回索引1处的元素。这相当于Name oneperson=names.get(1)
或者你可以喜欢objectoneperson=names.get(1)代码>。