找到整数并返回在数组中它的位置 [英] find integer and return its position in the array

查看:139
本文介绍了找到整数并返回在数组中它的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要产生code,将执行以下操作对我来说:


  • 插入一个元素到数组

  • 找到阵列中的元件的位置

由于某些原因(我想这可能是因为插入排序方法),我没有得到预期的效果。如果我举个例子,插入4个元素的数组长度5插入应该在指数3。然而最后一个元素,此刻,我得到的位置4,这是不对的。

我的问题是我有什么用,使这个code的工作,因为它应该,当我试图使用整数而不是 INT (检查是否有的下一个位置,从停止插入排序执行)我得到一个 NullPointerException异常(我替换所有 INT [] 整数[] 不改变任何东西)。

 公共类arrayImplementation实现programInterface
{
    INT指针= 0;
    INT数= -1;
    静态INT [] theArray;    公共arrayImplementation(INT大小){
        theArray =新INT [大小]
    }    @覆盖
    公共无效插入(INT键){
        theArray [指针] =键;
        如果(指针!= 0){
        插入排序(theArray);
        }
        指针++;
    }    @覆盖
    公众诠释发现(INT键){        INT低= 0;
        INT高= theArray.length - 1;        而(高> =低){
            INT中旬=(低+高)/ 2;
            如果(theArray [MID] ==键){
                数=中旬;
                返回数;
            }
            如果(theArray [MID]<键){
                低=中旬+ 1;
            }
            如果(theArray [中间]≥键){
                高=中旬 - 1;
            }
        }
        返回数;
    }    @覆盖
    公共无效删除(INT键){}    公共无效插入排序(INT [] theArray){
        的for(int i = 1; I< theArray.length;我++){
             INT TEMP = theArray [I]
             INT J =;
            而(J> 0安培;&安培;温度< theArray [J - 1]){
                theArray [J] = theArray [J - 1];
                当J = J - 1;
            }
            theArray [J] =温度;
        }
    }    公共静态无效的主要(字符串ARG []){
        arrayImplementation arrImp =新arrayImplementation(5);
        arrImp.insert(1);
        arrImp.insert(2);
        arrImp.insert(3);
        arrImp.insert(7);
        的System.out.println(arrImp.find(7));
        的for(int i = 0; I< theArray.length;我++){
        的System.out.println(theArray [I] +,);
        }
    }
}


解决方案

插入 INT 到阵列的同时排序导致失去一些价值。 INT ARR [] =新INT [5] 意味着你的数组有5号里面全是0​​({0,0,0,0,0}) 。如果你同时加入一些对它们进行排序,你会失去你的一些int值。你应该将它们全部排序后您的阵列。当你运行你自己的code,你会看到,没有2你的数组中为止。如果您使用Integer对象,而不是'诠释',就会出现空,而不是0。我写这在结束时发送空值我自己的排序方法。试试下面code。

 进口java.util.Arrays中;公共类ArrayImplementation {    INT指针= 0;
    静态整数[] theArray;    公共ArrayImplementation(INT大小){
        theArray =新的整数【尺寸】;
    }    公共无效插入(int i)以{
        theArray [指针++] =我;
    }    公众诠释查找(int i)以{
        INT C = 0;
        对于(INT N:theArray){
            如果(N == I)
                返回℃;
            C ++;
        }
        返回-1;
    }
    公共无效的sort(){        Arrays.sort(theArray,新的比较<整数GT;(){
            @覆盖
            公众诠释比较(整数x,整数Y)
            {
                如果(X == NULL ||ÿ== NULL)
                    返回1; //在数组的末尾发送空值。
                返回说明X - Y;
            }
        });
    }
    公共静态无效的主要(字符串ARG []){
        ArrayImplementation arrImp =新ArrayImplementation(5);
        arrImp.insert(1);
        arrImp.insert(2);
        arrImp.insert(3);
        arrImp.insert(7);
        arrImp.sort(); //你应该在排序插入结束。
        的for(int i = 0; I< theArray.length;我++){
            的System.out.println(theArray [I] +,);
        }        的System.out.println();
        的System.out.println(arrImp.find(7));
    }
}

7在3阵列中的停留。


  

打印:

  1,
2,
3,
7,
空值3


I want to produce the code which will do the following for me:

  • insert an element into array
  • find the position of the element in the array

For some reason (I think this might be because of the insertionSort method), I am not getting the desired results. If I, for example, insert 4 elements to the array with a length 5 the last element inserted should be at index 3. However, at the moment the position that I got is 4, which is wrong.

My question is what I have to use to make this code works as it should, when I am trying to use Integer instead of int (to check if there is null at next position to stop the insertionSort from executing) I am getting a NullPointerException (I replace all int[] to Integer[] without changing anything else).

public class arrayImplementation implements programInterface
{
    int pointer = 0;
    int number = -1;
    static int[] theArray;

    public arrayImplementation(int size) {
        theArray = new int[size];
    }

    @Override
    public void insert(int key) {
        theArray[pointer] = key;
        if(pointer != 0){
        insertionSort(theArray);
        }
        pointer++;
    }

    @Override
    public int find(int key) {

        int low = 0;
        int high = theArray.length - 1;

        while (high >= low) {
            int mid = (low + high) / 2;
            if (theArray[mid] == key) {
                number = mid;
                return number;
            }
            if (theArray[mid] < key) {
                low = mid + 1;
            }
            if (theArray[mid] > key) {
                high = mid - 1;
            }
        }
        return number;
    }

    @Override
    public void delete(int key) {}

    public void insertionSort(int[] theArray) {
        for (int i = 1; i < theArray.length; i++) {
             int temp = theArray[i];
             int j = i;
            while (j > 0 && temp < theArray[j - 1]) {
                theArray[j] = theArray[j - 1];
                j = j - 1;
            }
            theArray[j] = temp;
        }
    }

    public static void main(String arg[]) {
        arrayImplementation arrImp = new arrayImplementation(5);
        arrImp.insert(1);
        arrImp.insert(2);
        arrImp.insert(3);
        arrImp.insert(7);
        System.out.println(arrImp.find(7));
        for(int i = 0;i<theArray.length;i++){
        System.out.println(theArray[i]+", ");
        }
    }
}

解决方案

"Inserting int into your array with sorting in the same time" causes losing some value. int arr[] = new int [5] means your array has "5" numbers which are all "0"({0,0,0,0,0}). If you sort them while adding number you will lose your some int value. You should sort your array after adding them all. When you run your own code, you will see that there is not "2" in your array. If you use Integer object instead of 'int', there will be null instead of '0'. I write my own sorting method which send null values at the end. Try code below.

import java.util.Arrays;

public class ArrayImplementation {

    int pointer = 0;
    static Integer[] theArray;

    public ArrayImplementation(int size) {
        theArray = new Integer[size];
    }

    public void insert(int i) {
        theArray[pointer++] = i;
    }

    public int find(int i) {
        int c = 0;
        for (int n : theArray) {
            if (n == i)
                return c;
            c++;
        } 
        return -1;
    }
    public void sort () {

        Arrays.sort(theArray, new Comparator<Integer>() {
            @Override
            public int compare(Integer x, Integer y)
            {
                if (x == null || y == null)
                    return 1; // sends null value at the end of the array.
                return x - y;
            }
        });
    }
    public static void main(String arg[]) {
        ArrayImplementation arrImp = new ArrayImplementation(5);
        arrImp.insert(1);
        arrImp.insert(2);
        arrImp.insert(3);
        arrImp.insert(7);
        arrImp.sort(); // you should sort at the end of insertion. 
        for (int i = 0; i < theArray.length; i++) {
            System.out.println(theArray[i] + ", ");
        }

        System.out.println("");
        System.out.println(arrImp.find(7));
    }
}

"7" stays at 3 in the array.

prints:

1, 
2, 
3, 
7,
null

3

这篇关于找到整数并返回在数组中它的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆