改善我的方法的性能

时间:2018-05-28 03:11:35

标签: java arrays performance hashmap scalability

晚上好,

我有一些相当庞大且难以理解的代码,我正在努力简化,是否有任何可以推荐的良好实践

public static String getPropertybyId(){
       Scanner reader = new Scanner(System.in);  // Reading from System.in
   System.out.println("Enter an id number to search properties: ");
   int n = reader.nextInt(); // Scans the next token of the input as an int.
  reader.close();
  System.out.println(id.get(n) +", "+ property_address.get(n) +", "+ first_name.get(n) +", "+ last_name.get(n) +", "+
  email.get(n) +", "+
  owner_address.get(n) +", "+
  price.get(n)+", "+
  date_sold.get(n));


  return id.get(n) +" ,"+ property_address.get(n) +" ,"+ first_name.get(n) +" ,"+ last_name.get(n) +" ,"+
  email.get(n) +" ,"+
  owner_address.get(n) +" ,"+
  price.get(n);
}


    public static void getTopProperties(){
  price.remove(0);
  int n = 0;
  while (n < price.size()){
    price.set(n,((price.get(n)).substring(1)));
    n = n + 1;
  }

     Collections.sort(price);
 Scanner reader = new Scanner(System.in);  // Reading from System.in
 System.out.println("Enter a number for the list size of top properties: ");
 int user = reader.nextInt(); // Scans the next token of the input as an int.
 shrinkTo(price, user);
 reader.close();
 System.out.println(price);

  }

  public static void shrinkTo(List list, int newSize) {
int size = list.size();
if (newSize >= size) return;
for (int i = newSize; i < size; i++) {
    list.remove(list.size() - 1);
}

对于getPropertybyID,我将根据ID号返回属性的所有详细信息,例如所有者名称和价格。 有没有办法将不同的数组一起hashmap或链接在一起,所以我没有这么长的return语句?

这是我的阵列:

    private static List<String> id = new ArrayList<String>();
  private static List<String> property_address = new ArrayList<String>();
  private static List<String> first_name = new ArrayList<String>();
      private static List<String> last_name = new ArrayList<String>();
  private static List<String> email = new ArrayList<String>();
  private static List<String> owner_address = new ArrayList<String>();
  private static List<String> price = new ArrayList<String>();
  private static List<String> date_sold = new ArrayList<String>();

for getTopProperties我想删除对另一个类的调用并嵌入那个额外的代码,但我不知道如何。

很抱歉,如果我的代码在初学者中不是很好

3 个答案:

答案 0 :(得分:0)

您需要使用OOP。面向对象编程将允许您简化整个问题类型。您的代码将缩小并变得更简单/可读。

阅读here并感到惊讶。

答案 1 :(得分:0)

你可以将整个事物传递给一个单独的方法进行String转换并在返回之前将其返回

像这样......

    private String getPropertyDetials(int index) {
            return id.get(index) +" ,"+ property_address.get(index) +" ,"+ first_name.get(index) +" ,"+ last_name.get(index) +" ,"+
  email.get(index) +" ,"+
  owner_address.get(index) +" ,"+
  price.get(index);
        }

进一步提高可读性,考虑创建模型类并重写ToString方法以执行上述操作,并且像FileNotFoundEx一样,您需要查看OOP概念

答案 2 :(得分:0)

我会避免重复String构建练习,而是使用格式化来构建String一次。此外,close Scanner包裹System.in 全球 )是个不错的主意。相反,将读者与方法分离;并将其传入。或者更好的是,将问题分开并传入n。像,

public static String getPropertybyId(int n) {
    String fmt = "%s, %s, %s, %s, %s, %s, %s, %s";
    String msg = String.format(fmt, id.get(n), property_address.get(n), 
            first_name.get(n), last_name.get(n), email.get(n), 
            owner_address.get(n), price.get(n), date_sold.get(n));
    System.out.println(msg);
    return msg;
}