2017-02-27 66 views
2

我的任务是创建一个与拼接具有相同功能的方法,但是我无法将适当的值存入适当的索引。我的代码工作如下,如何在Java中编写类似拼接的方法

public void PlaceElementAt(int newValue, int index) throws ArrayIndexOutOfBoundsException { 

    //check that index is valid 
    if (index >= 0 && index <= data.length) { //Checks that the index is within position 0 and 7 by default. 
     System.out.println ("Index is valid"); //returns index is valid if so 
    } 
    //increase size if necessary 
    if (data.length == numElements) { //checking if the number of elements is filling the spaces 
     doubleCapacity();    // calls upon the double capacity method if it is 
    } 
    if (numElements==0) { 
     data[numElements] = newValue; 
     System.out.println ("Element: " + data[numElements] + " at index: " + index); 
     numElements++; 
    } 
    //shuffle values down from index 
    else { 
    int bottompos = numElements-1; 
    int loopcount = numElements-index; 
    int NewBottom = numElements+1; 
    for (int i=0; i<loopcount; i++){ 
     data[bottompos]=data[NewBottom]; 
     bottompos--; 
     NewBottom--; 
    } 
     //insert newValue at index 
     data[numElements] = newValue; 
     System.out.println ("Element: " + data[numElements] +" at index: " + index);  
     numElements++; 

    } 
} 

我后来在我的主要方法中给出的命令时,我的问题很明显。

myData.PlaceElementAt(3,0)

myData.PlaceElementAt(2,5)

myData.PlaceElementAt(7,3)

一次检查我的断点,我看到的值被添加到数组中,但是它们是从索引0开始逐个添加的。任何建议都会大大有所帮助。

+3

Java没有一个拼接的方法。 – shmosel

+0

@shmosel我认为他正在努力创造一个。至少,这是我从他的问题描述中收集到的。 –

+0

@HypnicJerk请参阅[修订历史记录](http://stackoverflow.com/posts/42496438/revisions)了解我的评论。 – shmosel

回答

1

我对你的班级结构进行了一些假设(根据需要调整),但通常我会建议右移,因为它会简化整个过程。基本上我们有一个数组,其最大允许尺寸由MAX给出,当前尺寸由size给出。任何时候我们添加到数组中,我们将size的值增加1(例如插入或添加到我们列表的后面)。现在,假设我们想要在index处插入value。这将需要将此指数右侧的所有元素右移1,然后将我们的value插入我们所做的空间。在进行任何插入之前,我们首先需要检查是否有足够的空间插入项目。如果没有足够的空间,我们需要分配额外的空间,禁止插入或其他选择。在你的情况下,它看起来像你想分配更多的空间。

class MyList 
{ 
    private int MAX = 6; 
    private int size = 0; 
    private int[] array; 

    public MyList() 
    { 
    array = new int[MAX]; 
    } 

    public void placeElementAt(int value, int index) 
    { 
    if (size == 0) 
    { 
     // If size is 0, just insert the value at index 0. 
     array[size++] = value; 
     return; 
    } 

    if (index < 0 || index >= size) 
    { 
     // Index is out of bounds. 
     System.out.println("Invalid index."); 
     return; 
    } 

    if (size >= MAX) 
    { 
     // Max capacity reached -> allocate more space. 
     doubleCapacity(); 
    } 

    // Shift all elements at and above index right by 1. 
    for (int i = size - 1; i >= index; i--) 
    { 
     array[i + 1] = array[i]; 
    } 

    // Insert element. 
    array[index] = value; 
    size++; 
    } 

    public void doubleCapacity() 
    { 
    int[] newArray = new int[MAX * 2]; 

    // Copy old elements to new array. 
    for (int i = 0; i < size; i++) 
    { 
     newArray[i] = array[i]; 
    } 

    // Double MAX to reflect new array. 
    MAX *= 2; 
    array = newArray; 

    System.out.println("Doubled"); 
    } 

    public void add(int value) 
    { 
    if (size >= MAX) 
    { 
     // Max capacity reached -> allocate more space. 
     doubleCapacity(); 
    } 

    // Add the element to the back of the list. 
    array[size++] = value; 
    } 

    public void print() 
    { 
    for (int i = 0; i < size; i++) 
    { 
     System.out.print(array[i] + " "); 
    } 
    System.out.println(); 
    } 

    public static void main(String[] args) 
    { 
    MyList data = new MyList(); 
    data.placeElementAt(1, 0); 
    data.print(); 
    data.placeElementAt(2, 0); 
    data.print(); 
    data.placeElementAt(3, 0); 
    data.print(); 
    data.placeElementAt(5, 0); 
    data.print(); 
    data.placeElementAt(3, 0); 
    data.print(); 
    data.placeElementAt(9, 0); 
    data.print(); 
    data.placeElementAt(4, 0); 
    data.print(); 
    data.placeElementAt(6, 0); 
    data.print(); 
    } 
} 

这个程序(初始MAX = 6)是的输出...

1 
2 1 
3 2 1 
5 3 2 1 
3 5 3 2 1 
9 3 5 3 2 1 
Doubled 
4 9 3 5 3 2 1 
6 4 9 3 5 3 2 1 
+0

这几乎可以解决我的问题,但是当我在数组中的最后一个数字似乎是重复的。例如,这个命令列表myData.insertElementAt(1,0); myData.insertElementAt(2,0); myData.insertElementAt(3,0); myData.insertElementAt(5,0);我的数组变成5,3,2,1,1,0 – Serkuto

+0

你能在你的问题中发布你的更新代码吗?当我运行以下代码: 'data.placeElementAt(1,0);' 'data.print();' 'data.placeElementAt(2,0);' 'data.print();' 'data.placeElementAt(3,0);'' data.print();'' data.placeElementAt(5,0);'' data.print();' 我得到作为输出: '1', '2 1', '3 2 1', '5 3 2 1' – dwhite5914

+0

我还编辑了我的原始代码以包含'doubleCapacity'方法,以防这是您的问题的一部分,并添加了一些样本输出。 – dwhite5914

0

好吧,看到你的代码,你正在收到一个“索引”参数。但是你没有使用索引来插入你的数组。

您的代码:

data[numElements] = newValue; 
numElements++; 

试试这个:

data[index] = newValue; 
numElements++; 

我认为这将解决这个问题,但后来你必须处理在指定的索引已经有一个元素的情况。

相关问题