Java 这种情况下的最佳阵列
我有一个问题,就是如何选择用于本例的阵列。有许多数组,如list、collection等。我想在数组中保留两个这样的数字(这里的数字是随机的): (多行2列) 但也具有此数组的功能,可以轻松地获取第一个数组行并将其放在末尾。你能建议我在这种情况下使用什么Java 这种情况下的最佳阵列,java,arrays,Java,Arrays,我有一个问题,就是如何选择用于本例的阵列。有许多数组,如list、collection等。我想在数组中保留两个这样的数字(这里的数字是随机的): (多行2列) 但也具有此数组的功能,可以轻松地获取第一个数组行并将其放在末尾。你能建议我在这种情况下使用什么 p、 我是新来的,所以我想选择最好的选项,这就是我来这里的原因。你最好创建一个类,比如说,结对: class Pair { private int i1; private int i2; public Pair(int
p、 我是新来的,所以我想选择最好的选项,这就是我来这里的原因。你最好创建一个类,比如说,
结对
:
class Pair {
private int i1;
private int i2;
public Pair(int i1, int i2) { this.i1 = i1; this.i2 = i2; }
public int getI1() { return i1; }
public int getI2() { return i2; }
}
然后根据您的特殊需要在
ArrayList
、HashSet
等之间进行选择。您最好创建一个类,比如说,Pair
:
class Pair {
private int i1;
private int i2;
public Pair(int i1, int i2) { this.i1 = i1; this.i2 = i2; }
public int getI1() { return i1; }
public int getI2() { return i2; }
}
然后根据您的特殊需要在
ArrayList
、HashSet
等之间进行选择。取决于您所说的轻松。易于编程或高性能。
既然你是新来的,我想你是在找第一个。我会坚持使用ArrayList
或ArrayList
,其中Pair是如下所示的自定义类:
public class Pair<A, B>
{
public Pair(A a, B b) { this.a = a; this.b = b; }
public A a;
public B b;
}
List<Pair<Integer, Integer>> list = new ArrayList<Pair<Integer, Integer>>();
list.add(new Pair<Integer, Integer>(3, 5));
list.add(new Pair<Integer, Integer>(7, 1));
// now take the first and put it at the end:
list.add(list.remove(0));
公共类对
{
公共对(aa,bb){this.A=A;this.B=B;}
公共A;
公共B B;
}
然后像这样使用:
public class Pair<A, B>
{
public Pair(A a, B b) { this.a = a; this.b = b; }
public A a;
public B b;
}
List<Pair<Integer, Integer>> list = new ArrayList<Pair<Integer, Integer>>();
list.add(new Pair<Integer, Integer>(3, 5));
list.add(new Pair<Integer, Integer>(7, 1));
// now take the first and put it at the end:
list.add(list.remove(0));
List List=new ArrayList();
添加(新的一对(3,5));
增加(新的一对(7,1));
//现在把第一个放在最后:
list.add(list.remove(0));
编辑:如果将第一个元素移动到末尾的性能是瓶颈,并且您希望它快速运行,那么就使用LinkedList。这将是一个O(1)操作,而使用ArrayList执行此操作将是一个O(n)操作。取决于您所说的“轻松”的意思。易于编程或高性能。 既然你是新来的,我想你是在找第一个。我会坚持使用
ArrayList
或ArrayList
,其中Pair是如下所示的自定义类:
public class Pair<A, B>
{
public Pair(A a, B b) { this.a = a; this.b = b; }
public A a;
public B b;
}
List<Pair<Integer, Integer>> list = new ArrayList<Pair<Integer, Integer>>();
list.add(new Pair<Integer, Integer>(3, 5));
list.add(new Pair<Integer, Integer>(7, 1));
// now take the first and put it at the end:
list.add(list.remove(0));
公共类对
{
公共对(aa,bb){this.A=A;this.B=B;}
公共A;
公共B B;
}
然后像这样使用:
public class Pair<A, B>
{
public Pair(A a, B b) { this.a = a; this.b = b; }
public A a;
public B b;
}
List<Pair<Integer, Integer>> list = new ArrayList<Pair<Integer, Integer>>();
list.add(new Pair<Integer, Integer>(3, 5));
list.add(new Pair<Integer, Integer>(7, 1));
// now take the first and put it at the end:
list.add(list.remove(0));
List List=new ArrayList();
添加(新的一对(3,5));
增加(新的一对(7,1));
//现在把第一个放在最后:
list.add(list.remove(0));
编辑:如果将第一个元素移动到末尾的性能是瓶颈,并且您希望它快速运行,那么就使用LinkedList。这将是一个O(1)操作,而使用ArrayList执行此操作将是一个O(n)操作。首先,您谈论的是集合,而不是数组:数组是一种特定的集合,也是最原始的集合。如果在创建集合时已知集合中的元素数,则应直接使用数组,并且此后不再更改
如果您的需求是从集合的开头移除元素并在末尾插入,那么您应该使用a或a,并将表示这对数字的对象作为泛型类型参数。这些数据结构经过优化,可以快速插入和从末端移除。首先,您讨论的是集合,而不是数组:数组是一种特定的集合,也是最原始的集合。如果在创建集合时已知集合中的元素数,则应直接使用数组,并且此后不再更改
如果您的需求是从集合的开头移除元素并在末尾插入,那么您应该使用a或a,并将表示这对数字的对象作为泛型类型参数。这些数据结构经过优化,可快速插入和从末端移除。您询问的是一般的数据结构;数组只是一种特定的结构。你用它做什么?只有一个数组-数组。您提到的所有其他“数组”都称为“集合”。对于您的特定情况,请尝试使用HashMaps.:)您可以使用列表中的数组,如
List arr
,因为您知道第一个维度是一个固定值(即2)。这个列表会给你一个可变的第二维度;数组只是一种特定的结构。你用它做什么?只有一个数组-数组。您提到的所有其他“数组”都称为“集合”。对于您的特定情况,请尝试使用HashMaps.:)您可以使用列表中的数组,如List arr
,因为您知道第一个维度是一个固定值(即2)。这个列表会给你一个可变的第二维度。嗯,我之前做了一些类似于其他类的东西,也许它会起作用,谢谢我会尝试+1:事实上,我喜欢非泛型版本,因为JVM会不断地进行装箱和拆箱。嗯,我在前面用其他类做了一些类似的东西,也许它会工作,谢谢我尝试+1:事实上,我喜欢非泛型版本,因为JVM会不断地进行装箱和拆箱。