简体   繁体   English

在 Java 中操作数组列表的部分

[英]Manipulating section of arraylist in Java

I have a matrix of W of large row and column dimension.我有一个大行和列维度的W矩阵。 Each of the row represents feature values(filled with double value).And the matrix is constructed as:每一行代表特征值(填充双值)。矩阵构造为:

  Hashmap<Integer,Arraylist<Double>> W = new Hashmap<Integer,Arraylist<Double>>();

While making computation, I need to take certain portion of each of the rows and update them in matrix.在进行计算时,我需要取每一行的某些部分并在矩阵中更新它们。 I looked for subList method in Arraylist .我在Arraylist寻找subList方法。 But the problem is it returns only list but I am in need of arraylist.但问题是它只返回列表,但我需要数组列表。 Because many of the methods that I have already implemented take <Arraylist> as argument.因为我已经实现的许多方法都以<Arraylist>作为参数。 So what can be the solution for this case?那么对于这种情况有什么解决办法呢?

Example例子

 w1 = [1,3 ,4,6,9,1,34,5,21,5,2,5,1]
 w11 = [1,3,4]
 w11 = w11 +1 = [2,4,5]
 This changes w1 to = [2,4 ,5,6,9,1,34,5,21,5,2,5,1]

I looked for subList method in Arraylist.But the problem is it returns only list but i am in need of arraylist我在 Arraylist 中寻找 subList 方法。但问题是它只返回列表,但我需要 arraylist

That is not a problem at all.这根本不是问题。 In fact, you should change your code to use List wherever possible.事实上,您应该尽可能更改代码以使用List

A List is an interface which concrete types such as the ArrayList implement. ListArrayList等具体类型实现的接口。 The below is perfectly valid:以下是完全有效的:

List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");

I recommend changing your W to this:我建议将您的W更改为:

Hashmap<Integer, List<Double>> W = new Hashmap<Integer, List<Double>>();

You could subclass ArrayList to provide a view of a slice of another ArrayList .您可以继承ArrayList以提供另一个ArrayList切片的视图。 Like this:像这样:

class ArrayListSlice<E> extends ArrayList<E> {
  private ArrayList<E> backing_list;
  private int start_idx;
  private int len;
  ArrayListSlice(ArrayList<E> backing_list, int start_idx, int len) {
    this.backing_list = backing_list;
    this.start_idx = start_idx;
    this.len = len;
  }
  public E get(int index) {
    if (index < 0 || idx >= len) throw new IndexOutOfBoundsException();
    return backing_list.get(start_idx + index);
  }
  ... set, others? ...
}

Then you can do w11 = new ArrayListSlice<Double>(w1, 0, 3) .然后你可以做w11 = new ArrayListSlice<Double>(w1, 0, 3) Any operations on w11 will appear in w1 , assuming you implement set correctly. w11上的任何操作都将出现在w1 ,假设您正确实现了set

You will probably need to implement most the methods of ArrayList to make this work.您可能需要实现 ArrayList 的大多数方法才能完成这项工作。 Some may work if they just rely on others, but that's hard to tell from the spec.如果他们只是依赖其他人,有些人可能会工作,但这很难从规范中看出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM