java.util.ArrayList.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(307)

本文整理了Java中java.util.ArrayList.<init>()方法的一些代码示例,展示了ArrayList.<init>()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ArrayList.<init>()方法的具体详情如下:
包路径:java.util.ArrayList
类名称:ArrayList
方法名:<init>

ArrayList.<init>介绍

[英]Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.
[中]构造一个空向量,使其内部数据数组的大小为10,其标准容量增量为零。

代码示例

canonical example by Tabnine

private void usingArrayList() {
 ArrayList<String> list = new ArrayList<>(Arrays.asList("cat", "cow", "dog"));
 list.add("fish");
 int size = list.size(); // size = 4
 list.set(size - 1, "horse"); // replacing the last element to "horse"
 String removed = list.remove(1); // removed = "cow"
 String second = list.get(1); // second = "dog"
}

代码示例来源:origin: stackoverflow.com

List<String> list = new ArrayList<String>();
//add some stuff
list.add("android");
list.add("apple");
String[] stringArray = list.toArray(new String[0]);

代码示例来源:origin: stackoverflow.com

List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");

String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);

for(String s : stockArr)
  System.out.println(s);

代码示例来源:origin: stackoverflow.com

ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");

代码示例来源:origin: stackoverflow.com

public class SortedList<E> extends AbstractList<E> {

  private ArrayList<E> internalList = new ArrayList<E>();

  // Note that add(E e) in AbstractList is calling this one
  @Override 
  public void add(int position, E e) {
    internalList.add(e);
    Collections.sort(internalList, null);
  }

  @Override
  public E get(int i) {
    return internalList.get(i);
  }

  @Override
  public int size() {
    return internalList.size();
  }

}

代码示例来源:origin: square/okhttp

private List<String> percentDecode(List<String> list, boolean plusIsSpace) {
 int size = list.size();
 List<String> result = new ArrayList<>(size);
 for (int i = 0; i < size; i++) {
  String s = list.get(i);
  result.add(s != null ? percentDecode(s, plusIsSpace) : null);
 }
 return Collections.unmodifiableList(result);
}

代码示例来源:origin: spring-projects/spring-framework

private static List<Element> prependWithSeparator(List<Element> elements) {
  List<Element> result = new ArrayList<>(elements);
  if (result.isEmpty() || !(result.get(0) instanceof Separator)) {
    result.add(0, SEPARATOR);
  }
  return Collections.unmodifiableList(result);
}

代码示例来源:origin: stackoverflow.com

List<String> strings = new ArrayList<String>()
strings.add("lol");
strings.add("cat");

Collections.sort(strings);
for (String s : strings) {
  System.out.println(s);
}
// Prints out "cat" and "lol"

代码示例来源:origin: google/guava

public SortedSetSubsetTestSetGenerator(
  TestSortedSetGenerator<E> delegate, Bound to, Bound from) {
 this.to = to;
 this.from = from;
 this.delegate = delegate;
 SortedSet<E> emptySet = delegate.create();
 this.comparator = emptySet.comparator();
 SampleElements<E> samples = delegate.samples();
 List<E> samplesList = new ArrayList<>(samples.asList());
 Collections.sort(samplesList, comparator);
 this.firstInclusive = samplesList.get(0);
 this.lastInclusive = samplesList.get(samplesList.size() - 1);
}

代码示例来源:origin: spring-projects/spring-framework

public static List<MessagingAdviceBean> createFromList(List<ControllerAdviceBean> beans) {
  List<MessagingAdviceBean> result = new ArrayList<>(beans.size());
  for (ControllerAdviceBean bean : beans) {
    result.add(new MessagingControllerAdviceBean(bean));
  }
  return result;
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * Adds a list/map reference.
 */
@Override
public int addRef(Object ref) {
  if (_refs == null)
    _refs = new ArrayList();
  _refs.add(ref);
  return _refs.size() - 1;
}

代码示例来源:origin: stackoverflow.com

ArrayList aList = new ArrayList();
//Add elements to ArrayList object
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");
Collections.reverse(aList);
System.out.println("After Reverse Order, ArrayList Contains : " + aList);

代码示例来源:origin: spring-projects/spring-framework

protected HandshakeInterceptor[] getInterceptors() {
  List<HandshakeInterceptor> interceptors = new ArrayList<>(this.interceptors.size() + 1);
  interceptors.addAll(this.interceptors);
  interceptors.add(new OriginHandshakeInterceptor(this.allowedOrigins));
  return interceptors.toArray(new HandshakeInterceptor[0]);
}

代码示例来源:origin: stackoverflow.com

String[] arr = new String[1];
 arr[0] = "rohit";
 List<String> newList = Arrays.asList(arr);
 // Will throw `UnsupportedOperationException
 // newList.add("jain"); // Can't do this.
 ArrayList<String> updatableList = new ArrayList<String>();
 updatableList.addAll(newList); 
 updatableList.add("jain"); // OK this is fine. 
 System.out.println(newList);       // Prints [rohit]
 System.out.println(updatableList); //Prints [rohit, jain]

代码示例来源:origin: stackoverflow.com

// Substitute appropriate type.
ArrayList<...> a = new ArrayList<...>();

// Add elements to list.

// Generate an iterator. Start just after the last element.
ListIterator li = a.listIterator(a.size());

// Iterate in reverse.
while(li.hasPrevious()) {
 System.out.println(li.previous());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testGenericListOfArrays() throws MalformedURLException {
  GenericBean<String> gb = new GenericBean<>();
  ArrayList<String[]> list = new ArrayList<>();
  list.add(new String[] {"str1", "str2"});
  gb.setListOfArrays(list);
  BeanWrapper bw = new BeanWrapperImpl(gb);
  bw.setPropertyValue("listOfArrays[0][1]", "str3 ");
  assertEquals("str3 ", bw.getPropertyValue("listOfArrays[0][1]"));
  assertEquals("str3 ", gb.getListOfArrays().get(0)[1]);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void moreThanMaxWorkers() {
  final List<Worker> list = new ArrayList<Worker>();
  SchedulerMultiWorkerSupport mws = (SchedulerMultiWorkerSupport)Schedulers.computation();
  mws.createWorkers(max * 2, new WorkerCallback() {
    @Override
    public void onWorker(int i, Worker w) {
      list.add(w);
    }
  });
  assertEquals(max * 2, list.size());
}

代码示例来源:origin: stackoverflow.com

List<Person> people = new ArrayList<>();
people.add(new Person("Alice"));
people.add(new Person("Bob"));    
System.out.println(people);

// Prints [Alice, Bob]

代码示例来源:origin: stackoverflow.com

ArrayList<String> aList = new ArrayList<String>();
aList.add("One");
String element = aList.get(0); // no cast needed
System.out.println("Got one: " + element);

代码示例来源:origin: square/okhttp

public static List<String> alpnProtocolNames(List<Protocol> protocols) {
 List<String> names = new ArrayList<>(protocols.size());
 for (int i = 0, size = protocols.size(); i < size; i++) {
  Protocol protocol = protocols.get(i);
  if (protocol == Protocol.HTTP_1_0) continue; // No HTTP/1.0 for ALPN.
  names.add(protocol.toString());
 }
 return names;
}

相关文章