自从Microsoft SQL Server 2005版开始,微软在Transact-SQL中加入了一个新函数,叫做NEWSEQUENTIALID(),用来生成主键增大的GUID,但一旦服务器重新启动,其再次生成的GUID可能反而变小(但仍然保持唯一)。这在很大程度上提高了索引的性能,但并不能保证所生成的GUID已知增大。这个函数产生的GUID很简单就可以预测,因此不适合用于安全目的。
publicbooleanaddAll(Collection<? extends E> c) { if (c == null) thrownewNullPointerException(); if (c == this) thrownewIllegalArgumentException(); booleanmodified=false; for (E e : c) if (add(e)) modified = true; return modified; }
publicintindexOf(Object o) { if (o == null) {//如果传入的参数是null,则循环遍历数组,找到第一个为null的值,返回它的索引 for (inti=0; i < size; i++) if (elementData[i]==null) return i; } else {//否则,遍历数组,查看两个对象是否相等;比对时需要注意equals方法有没有被重写 for (inti=0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1;//如果数组中找不到该值,则返回-1 }
返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1
1 2 3 4 5 6 7 8 9 10 11 12
publicintlastIndexOf(Object o) {//倒叙遍历数组 if (o == null) { for (inti= size-1; i >= 0; i--) if (elementData[i]==null) return i; } else { for (inti= size-1; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1; }
public Object[] toArray() { return Arrays.copyOf(elementData, size); }
转化为对象数组,数组中元素顺序和在列表中时一致;重写分配了内存空间,修改该数组不影响原列表;
需要注意的是,这里仍为浅拷贝,修改元素的内容,会影响原列表中元素的内容
1 2 3 4 5 6 7 8 9 10
@SuppressWarnings("unchecked") public <T> T[] toArray(T[] a) { if (a.length < size) // Make a new array of a's runtime type, but my contents: return (T[]) Arrays.copyOf(elementData, size, a.getClass()); System.arraycopy(elementData, 0, a, 0, size); if (a.length > size) a[size] = null; return a; }
按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组
获取指定位置的元素
1 2 3 4 5 6 7 8 9 10
@SuppressWarnings("unchecked") E elementData(int index) { return (E) elementData[index]; }
public E get(int index) { rangeCheck(index);//检查传入的索引参数是否超出列表的最大长度,如果超出,抛出越界异常IndexOutOfBoundsException return elementData(index); }
设值
1 2 3 4 5 6 7
public E set(int index, E element) { rangeCheck(index); EoldValue= elementData(index); elementData[index] = element; return oldValue; }
intnumMoved= size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // 将数组中最后一个位置的元素置空,使垃圾回收起效,此处可做参考
return oldValue; }
从列表中指定位置移除某个元素,列表的capacity容量不变,但是size减1
该方法返回移除的元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicbooleanremove(Object o) { if (o == null) { for (intindex=0; index < size; index++) if (elementData[index] == null) { fastRemove(index); returntrue; } } else { for (intindex=0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); returntrue; } } returnfalse; }
移除列表中第一个对象o,成功返回true
1 2 3 4 5 6 7
privatevoidfastRemove(int index) { modCount++; intnumMoved= size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work }
privatevoidwriteObject(java.io.ObjectOutputStream s) throws java.io.IOException{ // Write out element count, and any hidden stuff intexpectedModCount= modCount; s.defaultWriteObject();
// Write out size as capacity for behavioural compatibility with clone() s.writeInt(size);
// Write out all elements in the proper order. for (int i=0; i<size; i++) { s.writeObject(elementData[i]); }
if (modCount != expectedModCount) { //防止在ArrayList对象序列化期间修改了ArrayList thrownewConcurrentModificationException(); } }
SpringBoot 下使用 Junit 进行单元测试时,报错:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test;
信息: Could not detect default configuration classes for test class [com.qinghuazs.starlight.oss.AliOSSBucketClientTest]: AliOSSBucketClientTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration. java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
at org.springframework.util.Assert.state(Assert.java:73) at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:240) at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:153) at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:395) at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:312) at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:265) at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:108) at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:97) at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:139) at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:124) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:151) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:142) at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104) at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33) at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
/** * Static helper that can be used to run a {@link SpringApplication} from the * specified source using default settings. * @param primarySource the primary source to load * @param args the application arguments (usually passed from a Java main method) * @return the running {@link ApplicationContext} */ publicstatic ConfigurableApplicationContext run(Class<?> primarySource, String... args) { return run(newClass<?>[] { primarySource }, args); }
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=20 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/usr/local/soft/zookeeper/zookeeper-3.4.12/data dataLogDir=/usr/local/soft/zookeeper/zookeeper-3.4.12/logs # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 server.1=127.0.0.1:2888:3888