Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将数组排序为三个不同的ArrayLinearList_Java_Arrays_Sorting - Fatal编程技术网

Java 将数组排序为三个不同的ArrayLinearList

Java 将数组排序为三个不同的ArrayLinearList,java,arrays,sorting,Java,Arrays,Sorting,尝试将.txt文件中的数据排序为三个不同的ArrayLinear列表。但问题在于他们的能力。无论何时调用.add函数,容量都会加倍 } 这是ArrayLinear列表代码 protected Object[] element; // array of elements protected static int size; // number of elements in array protected static ArrayLinearList theObject; // construct

尝试将.txt文件中的数据排序为三个不同的ArrayLinear列表。但问题在于他们的能力。无论何时调用.add函数,容量都会加倍

} 这是ArrayLinear列表代码

protected Object[] element; // array of elements
protected static int size; // number of elements in array
protected static ArrayLinearList theObject;

// constructors
/**
 * create a list with initial capacity initialCapacity
 * 
 * @throws IllegalArgumentException
 *             when initialCapacity < 1
 */
public ArrayLinearList(int initialCapacity) {
    if (initialCapacity < 1)
        throw new IllegalArgumentException("initialCapacity must be >= 1");
    // size has the default initial value of 0
    element = new Object[initialCapacity];
}

/** create a list with initial capacity 10 */
public ArrayLinearList() {// use default capacity of 10
    this(10);
}

// methods
/** @return true iff list is empty */
public boolean isEmpty() {
    return size == 0;
}

/** @return current number of elements in list */
public int size() {
    return size;
}

/**
 * @throws IndexOutOfBoundsException
 *             when index is not between 0 and size - 1
 */
void checkIndex(int index) {
    if (index < 0 || index >= size)
        throw new IndexOutOfBoundsException("index = " + index + "  size = " + size);
}

/**
 * @return element with specified index
 * @throws IndexOutOfBoundsException
 *             when index is not between 0 and size - 1
 */
public Object get(int index) {
    checkIndex(index);
    return element[index];
}

/**
 * @return index of first occurrence of theElement, return -1 if theElement
 *         not in list
 */
public int indexOf(Object theElement) {
    // search element[] for theElement
    for (int i = 0; i < size; i++)
        if (element[i].equals(theElement))
            return i;

    // theElement not found
    return -1;
}

/**
 * Remove the element with specified index. All elements with higher index
 * have their index reduced by 1.
 * 
 * @throws IndexOutOfBoundsException
 *             when index is not between 0 and size - 1
 * @return removed element
 */
public Object remove(int index) {
    checkIndex(index);

    // valid index, shift elements with higher index
    Object removedElement = element[index];
    for (int i = index + 1; i < size; i++)
        element[i - 1] = element[i];

    element[--size] = null; // enable garbage collection
    return removedElement;
}

/**
 * Insert an element with specified index. All elements with equal or higher
 * index have their index increased by 1.
 * 
 * @throws IndexOutOfBoundsException
 *             when index is not between 0 and size
 */
public void add(int index, Object theElement) {
    if (index < 0 || index > size)
        // invalid list position
        throw new IndexOutOfBoundsException("index = " + index + "  size = " + size);

    // valid index, make sure we have space
    if (size == element.length)
        // no space, double capacity
        element = ChangeArrayLength.changeLength1D(element,2* size);

    // shift elements right one position
    for (int i = size - 1; i >= index; i--)
        element[i + 1] = element[i];

    element[index] = theElement;

    size++;
}

/** convert to a string */
public String toString() {
    StringBuffer s = new StringBuffer("[");

    // put elements into the buffer
    for (int i = 0; i < size; i++)
        if (element[i] == null)
            s.append("null, ");
        else
            s.append(element[i].toString() + ", ");

    if (size > 0)
        s.delete(s.length() - 2, s.length()); // remove last ", "

    s.append("]");

    // create equivalent String
    return new String(s);
}

/** create and return an iterator */
public Iterator iterator() {
    return new ArrayLinearListIterator((MyArrayList) this);
}

/** test program */
public static void main(String[] args) {
    // test default constructor
    LinearList x = new ArrayLinearList();

    // test size
    System.out.println("Initial size is " + x.size());

    // test isEmpty
    if (x.isEmpty())
        System.out.println("The list is empty");
    else
        System.out.println("The list is not empty");

    // test put
    x.add(0, new Integer(2));
    x.add(1, new Integer(6));
    x.add(0, new Integer(1));
    x.add(2, new Integer(4));
    System.out.println("List size is " + x.size());

    // test toString
    System.out.println("The list is " + x);

    // test indexOf
    int index = x.indexOf(new Integer(4));
    if (index < 0)
        System.out.println("4 not found");
    else
        System.out.println("The index of 4 is " + index);

    index = x.indexOf(new Integer(3));
    if (index < 0)
        System.out.println("3 not found");
    else
        System.out.println("The index of 3 is " + index);

    // test get
    System.out.println("Element at 0 is " + x.get(0));
    System.out.println("Element at 3 is " + x.get(3));

    // test remove
    System.out.println(x.remove(1) + " removed");
    System.out.println("The list is " + x);
    System.out.println(x.remove(2) + " removed");
    System.out.println("The list is " + x);

    if (x.isEmpty())
        System.out.println("The list is empty");
    else
        System.out.println("The list is not empty");

    System.out.println("List size is " + x.size());

}
受保护对象[]元素;//元素数组
受保护的静态整数大小;//数组中的元素数
受保护的静态阵列靠近对象列表;
//建设者
/**
*创建具有初始容量initialCapacity的列表
* 
*@galargumentException
*初始容量<1时
*/
公共阵列线性列表(int initialCapacity){
如果(初始容量<1)
抛出新的IllegalArgumentException(“初始容量必须大于等于1”);
//大小的默认初始值为0
元素=新对象[初始容量];
}
/**创建初始容量为10的列表*/
public ArrayLinearList(){//使用默认容量10
这(10);
}
//方法
/**@return-true iff列表为空*/
公共布尔值为空(){
返回大小==0;
}
/**@返回列表中元素的当前数目*/
公共整数大小(){
返回大小;
}
/**
*@throws IndexOutOfBoundsException
*当索引不在0和大小-1之间时
*/
无效检查索引(整数索引){
如果(索引<0 | |索引>=大小)
抛出新的IndexOutOfBoundsException(“index=“+index+”size=“+size”);
}
/**
*@返回具有指定索引的元素
*@throws IndexOutOfBoundsException
*当索引不在0和大小-1之间时
*/
公共对象get(int索引){
检查索引(索引);
返回元素[索引];
}
/**
*@返回元素第一次出现的索引,如果元素
*不在名单上
*/
公共int索引(对象元素){
//在元素[]中搜索该元素
对于(int i=0;i大小)
//列表位置无效
抛出新的IndexOutOfBoundsException(“index=“+index+”size=“+size”);
//有效的索引,确保我们有空间
if(size==element.length)
//没有空间,容量加倍
元素=ChangeArrayLength.ChangeLength 1d(元素,2*大小);
//将元件右移一个位置
对于(inti=size-1;i>=index;i--)
元素[i+1]=元素[i];
元素[索引]=元素;
大小++;
}
/**转换为字符串*/
公共字符串toString(){
StringBuffer s=新的StringBuffer(“[”);
//将元素放入缓冲区
对于(int i=0;i0)
s、 删除(s.length()-2,s.length());//删除最后一个“,”
s、 附加(“]”);
//创建等效字符串
返回新字符串;
}
/**创建并返回迭代器*/
公共迭代器迭代器(){
返回新的ArrayLinearListIterator((MyArrayList)this);
}
/**测试程序*/
公共静态void main(字符串[]args){
//测试默认构造函数
LinearList x=新的ArrayLinearList();
//测试尺寸
System.out.println(“初始大小为”+x.size());
//测试是空的
if(x.isEmpty())
System.out.println(“列表为空”);
其他的
System.out.println(“列表不是空的”);
//试举
x、 加(0,新整数(2));
x、 加(1,新整数(6));
x、 添加(0,新整数(1));
x、 加(2,新的整数(4));
System.out.println(“列表大小为”+x.size());
//测试串
System.out.println(“列表为”+x);
//测试指数
int index=x.indexOf(新整数(4));
如果(指数<0)
System.out.println(“4未找到”);
其他的
System.out.println(“4的索引是”+索引);
index=x.indexOf(新整数(3));
如果(指数<0)
System.out.println(“3未找到”);
其他的
System.out.println(“3的索引是”+索引);
//测试获取
System.out.println(“0处的元素是”+x.get(0));
System.out.println(“3处的元素为”+x.get(3));
//测试移除
System.out.println(x.remove(1)+“remove”);
System.out.println(“列表为”+x);
System.out.println(x.remove(2)+“remove”);
System.out.println(“列表为”+x);
if(x.isEmpty())
System.out.println(“列表为空”);
其他的
System.out.println(“列表不是空的”);
System.out.println(“列表大小为”+x.size());
}
} 提前感谢伟大的高级开发人员:对于使用ArrayLinearList的开发人员,这意味着提供更大的初始容量,以防止过多的增加

使用估算:

Path path = Paths.get("Subjects.txt");
int initialCapacity = (int)(Files.size(path) / 3);

对于实现ArrayLinearList的开发人员,他可以选择一个更大的默认初始容量,或者将数组大小增加
1000+array.length/4
,这样就不会占用那么多空间,并且最初不会重复增加那么多。

什么是
ArrayLinearList
?它不是来自标准库。@MuratKaragöz请参见图像中的第三个选项卡。所以它可能是一个定制的clas
Path path = Paths.get("Subjects.txt");
int initialCapacity = (int)(Files.size(path) / 3);