java 方法返回对象_如何从java中的方法返回对象的实例

我不知道如何从具有值的方法返回对象的实例。有什么办法将int类型转换为我的对象类型吗? 下面是一些说明我有:如何从java中的方法返回对象的实例

/* Design a class named Location for locating a maximal value and its location in a

two-dimensional array. The class contains:

-Public double type data field maxValue that stores the maximal value in a two-dimensional

array

-Public int type data fields row and column that store the maxValue indices in a

two-dimensional array

Write a the following method that returns the location of the largest element in a two

dimensional array:

public static Location locateLargest(double[][] a)

The return value is an instance of Location. Write a test program that prompts the user to

enter a two-dimensional array and displays the location of the largest element in the

array. Here is a sample run:

Enter the number of rows and columns in the array: 3 4

Enter the array:

23.5 35 2 10

4.5 3 45 3.5

35 44 5.5 9.6

The location of the largest element is 45 at (1, 2) */

这里是我的代码:

class Location {

public static double maxValue;

public static int row;

public static int column;

public static Location locateLargest(double[][] a) {

maxValue = a[0][0];

row = 0;

column = 0;

Location result = new Location();

for(int i = 0; i < a.length; i++) {

for(int j = 0; j < a[i].length; j++) {

if(a[i][j] > maxValue) {

maxValue = a[i][j];

row = i;

column = j;

if((i == a.length-1) && (j == a[i].length-1))

//Place indices of maxValue in result variable

}

else

continue;

}

}

return result;

}

}

我想我应该创建地点接受参数构造函数,但我不愿意,因为该指示并未说明如此。有没有其他方法可以做到这一点?由于

+0

是的,你可以添加一个参数化的构造函数并创建一个'Location'。我不认为这是不正确的。你应该与你的讲师确认:) –

+0

@TheLostMind是我认为是的,谢谢 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值