Java代码打印给定ArrayList的反向内容

huangapple go评论101阅读模式
英文:

Java Code That Prints Reverse of Given ArrayList

问题

这是我翻译好的内容:

所以我试图编写一个方法,该方法返回给定 ArrayList 的反向版本,但是它给我返回了索引错误,我不知道如何解决。我感谢您的帮助。这是我的代码:

import java.util.ArrayList;

public class question2 {

    public static ArrayList<Double> reverse(ArrayList<Double> arraylist) {
        ArrayList<Double> result = new ArrayList<Double>();
        for(int i=0; i<arraylist.size(); i++) {
            result.set((arraylist.size()) - 1 - i, arraylist.get(i));
        }
        return result;
    }
    
    public static void main(String[] args) {
        
        ArrayList<Double> doubles = new ArrayList<Double>();
        doubles.add(1.5);
        doubles.add(2.3);
        doubles.add(7.7);
        doubles.add(8.6);
        System.out.println(reverse(doubles));
        
    }
    
}

而这是错误信息:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 0
英文:

So I was trying to write a method which returns the reverse of given ArrayList but it gives me Index error and I don't know how to solve it. I appreciate your help. This is my code:

import java.util.ArrayList;

public class question2 {

public static ArrayList&lt;Double&gt; reverse(ArrayList&lt;Double&gt; arraylist) {
	ArrayList&lt;Double&gt; result = new ArrayList&lt;Double&gt;();
	for(int i=0; i&lt;arraylist.size(); i++) {
		result.set((arraylist.size()) - 1 - i, arraylist.get(i));
	}
	return result;
}
 
public static void main(String[] args) {
	
	ArrayList&lt;Double&gt; doubles = new ArrayList&lt;Double&gt;();
	doubles.add(1.5);
	doubles.add(2.3);
	doubles.add(7.7);
	doubles.add(8.6);
	System.out.println(reverse(doubles));
	
}

}

And this is the error:

Exception in thread &quot;main&quot; java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 0

答案1

得分: 1

public static ArrayList<Double> reverse(ArrayList<Double> arraylist) {
    ArrayList<Double> result = new ArrayList<Double>(arraylist.size());
    for(int i = arraylist.size() - 1; i >= 0; i--) {
        result.add(arraylist.get(i));
    }
    return result;
}
英文:
public static ArrayList&lt;Double&gt; reverse(ArrayList&lt;Double&gt; arraylist) {
       ArrayList&lt;Double&gt; result = new ArrayList&lt;Double&gt;(arraylist.size());
        for(int i=arraylist.size()-1; i&gt;=0; i—-) {
            result.add(arraylist.get(i));
        }
        return result;
     }

Hope this will work!

答案2

得分: 0

这里有一些你可能想尝试的内容。它不需要一个单独的 List,并且循环的次数只有将整个列表复制到另一个列表的一半多。

它的原理是随着循环索引接近中间,交换开头和结尾的值。

  • 设置 size = list.size();
  • 0 循环到 i < size/2
  • 将位置 i 处的值保存到一个变量中。
  • 将位置 i 处的值设置为位置 size - i - 1 处的值。
  • 将位置 size - i - 1 处的值设置为之前保存的值。

这将持续进行,直到循环达到中间位置。请注意,对于 偶数 大小的列表,最内部的值是相邻的并且被交换了。对于 奇数 大小的列表,最内部的项被忽略,因为它的位置是正确的。

英文:

Here is something you may want to try. It does not require a separate List and only loops half as many times as copying the entire list to another one.

It works by swapping the values at the beginning and the end as the loop index moves closer toward the middle.

  • set size = list.size();
  • iterate the loop from 0 to i &lt; size/2
  • save the value at i to a variable.
  • set the position at i to the value at size - i - 1
  • set the position at size - i - 1 to the saved value.

This will proceed until the loop has reached the middle. Note that for even sized lists, the inner most values are adjacent and swapped. For odd sized lists, the inner most item is ignored since it is correctly positioned.

答案3

得分: 0

你可以尝试我的代码,它会对你有用。

public static List<Double> reverse(ArrayList<Double> arraylist) {
    List<Double> result = Arrays.asList(new Double[arraylist.size()]);
    for (int i = 0; i < arraylist.size(); i++) {
        result.set((arraylist.size()) - 1 - i, arraylist.get(i));
    }
    return result;
}
英文:

Can you try my code , it will work for you

 public static List&lt;Double&gt; reverse(ArrayList&lt;Double&gt; arraylist) {
        List&lt;Double&gt; result = Arrays.asList(new Double[arraylist.size()]);
        for(int i=0; i&lt;arraylist.size(); i++) {
            result.set((arraylist.size()) - 1 - i, arraylist.get(i));
        }
        return result;
    }

答案4

得分: 0

public static String reverse(ArrayList<Double> arraylist) {
    ArrayList<Double> result = new ArrayList<Double>();
    for (int i = arraylist.size() - 1; i >= 0; i--) {
        result.add(arraylist.get(i));
    }
    return result.toString();
}

// reverse() 方法的返回类型应该是 char[] 或者 String,因为你从主方法在控制台上打印输出
英文:
public static String reverse(ArrayList&lt;Double&gt; arraylist) {
ArrayList&lt;Double&gt; result = new ArrayList&lt;Double&gt;();
for(int i=arraylist.size() -1; i&gt;=0; i--) {
    result.add(arraylist.get(i));
}
return result.toString();

}

the return type of reverse() method should be char[] or String as you are printing on the console from the main method

huangapple
  • 本文由 发表于 2020年10月8日 01:53:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64249679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定