将一串数字转换为数组

发布于 2024-12-10 06:42:54 字数 778 浏览 1 评论 0原文

我在创建将数字字符串转换为数组的程序时遇到问题。

我知道这里有一个类似的问题,但我所要做的只是一组数字,例如 [10 15 16 0 57 438 57 18]

这是我到目前为止所拥有的:

import java.util.Scanner;
public class Histogram {

   public static void main(String[] args) {
     Scanner keyboard = new Scanner(System.in);
     String numbers;


     numbers = keyboard.next();
     System.out.println(numbers);

     int [] n1 = new int [numbers.length()];



     for(int n = 0; n < numbers.length(); n++) {
        n1[n] = Integer.parseInt(numbers.split(" ")[n]);
     }


}
}

此代码抛出异常:

10 15 20 25 30
10
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Histogram.main(Histogram.java:18)

什么是我做错了吗?我想避免使用逗号来分隔元素。谁能解释为什么抛出异常以及如何修复它?我想了解比什么都重要。

i'm having problems creating a program that converts a string of numbers into an array.

i know there is a similar question on here, but all i have to work with is a set of numbers such as [10 15 16 0 57 438 57 18]

heres what i have so far:

import java.util.Scanner;
public class Histogram {

   public static void main(String[] args) {
     Scanner keyboard = new Scanner(System.in);
     String numbers;


     numbers = keyboard.next();
     System.out.println(numbers);

     int [] n1 = new int [numbers.length()];



     for(int n = 0; n < numbers.length(); n++) {
        n1[n] = Integer.parseInt(numbers.split(" ")[n]);
     }


}
}

this code throws throws an exception:

10 15 20 25 30
10
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Histogram.main(Histogram.java:18)

what am i doing wrong? i want to avoid using commas to separate the elements. can anyone explain why the exception is being thrown and how to fix it? i want to understand more than anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悟红尘 2024-12-17 06:42:54

您应该将字符串拆分一次并将数组存储在某处。

在这里,您将迭代到字符串中的字符数,而不是空格分隔的数字数。

int [] n1 = new int [numbers.length()];
for(int n = 0; n < numbers.length(); n++) {
   n1[n] = Integer.parseInt(numbers.split(" ")[n]);
}

更改为:

String[] parts = numbers.split(" ");
int[] n1 = new int[parts.length];
for(int n = 0; n < parts.length; n++) {
   n1[n] = Integer.parseInt(parts[n]);
}

在线查看其工作情况:ideone

You should split the string once and store the array somewhere.

Here you are iterating up to the number of characters in the string, not the number of space-separated numbers.

int [] n1 = new int [numbers.length()];
for(int n = 0; n < numbers.length(); n++) {
   n1[n] = Integer.parseInt(numbers.split(" ")[n]);
}

Change to:

String[] parts = numbers.split(" ");
int[] n1 = new int[parts.length];
for(int n = 0; n < parts.length; n++) {
   n1[n] = Integer.parseInt(parts[n]);
}

See it working online: ideone

终止放荡 2024-12-17 06:42:54

正在发生的事情是这样的:

     numbers = keyboard.next();
     System.out.println(numbers);

//This line is creating an array with the length in characters of the String which in this case is 14
     int [] n1 = new int [numbers.length()];


//Then When you split the number there is really only 5 elements, yet numbers.length() is 14, therefore when asking for the array on position 6 it causes the exception.
     for(int n = 0; n < numbers.length(); n++) {
        n1[n] = Integer.parseInt(numbers.split(" ")[n]);

另外,您应该只执行一次 number.split(" ") 操作,然后像这样访问它:

String [] stringParts = numbers.split(" "):

stringParts[n]

所以基本上您的 for 确实是这样的:

for(int n = 0; n < 14; n++)
{

}

然后numbers.split(" ") 返回只有 5 个元素,因为总共有 5 个数字,所以当你请求位置 6 时,会抛出异常,因为数组的大小只有 5。

Here is what is happening:

     numbers = keyboard.next();
     System.out.println(numbers);

//This line is creating an array with the length in characters of the String which in this case is 14
     int [] n1 = new int [numbers.length()];


//Then When you split the number there is really only 5 elements, yet numbers.length() is 14, therefore when asking for the array on position 6 it causes the exception.
     for(int n = 0; n < numbers.length(); n++) {
        n1[n] = Integer.parseInt(numbers.split(" ")[n]);

Aditionally you should just do this operation numbers.split(" ") once, and then just access it like this:

String [] stringParts = numbers.split(" "):

stringParts[n]

So basically your for is really something like this:

for(int n = 0; n < 14; n++)
{

}

and then numbers.split(" ") returns only 5 elements, since there is a total of 5 numbers, so when you ask for position 6, the exception is thrown as the array is only of size 5.

妞丶爷亲个 2024-12-17 06:42:54

我创建这个课程供我个人使用,但我认为它可以帮助您解决问题。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class ArrayUtils {

    private static final Logger log = LoggerFactory.getLogger(ArrayUtils.class);
    private static final Map<Class, Class> primitiveMapping = new HashMap<Class, Class>();
    private static final Map<Class, Method> primitiveParseMethodLookup = new HashMap<Class, Method>();
    private static final Map<Class, Method> primitiveArrayGetMethodLookup = new HashMap<Class, Method>();
    private static final Map<Class, Method> valueOfMethodLookup = new HashMap<Class, Method>();

    static {

        // Initialize primitive mappings
        primitiveMapping.put(boolean.class, Boolean.class);
        primitiveMapping.put(byte.class, Byte.class);
        primitiveMapping.put(short.class, Short.class);
        primitiveMapping.put(int.class, Integer.class);
        primitiveMapping.put(float.class, Float.class);
        primitiveMapping.put(long.class, Long.class);
        primitiveMapping.put(double.class, Double.class);

        // Initialize parse, valueOf and get method lookup
        // We do that in advance because the lookup of the method takes the longest time
        // Compared to the normal method call it's 20x higher
        // So we use just the reflective method call which takes double the time of a normal method call
        try {
            primitiveParseMethodLookup.put(boolean.class, Boolean.class.getMethod("parseBoolean", new Class[]{String.class}));
            primitiveParseMethodLookup.put(byte.class, Byte.class.getMethod("parseByte", new Class[]{String.class}));
            primitiveParseMethodLookup.put(short.class, Short.class.getMethod("parseShort", new Class[]{String.class}));
            primitiveParseMethodLookup.put(int.class, Integer.class.getMethod("parseInt", String.class));
            primitiveParseMethodLookup.put(float.class, Float.class.getMethod("parseFloat", String.class));
            primitiveParseMethodLookup.put(long.class, Long.class.getMethod("parseLong", String.class));
            primitiveParseMethodLookup.put(double.class, Double.class.getMethod("parseDouble", String.class));

            valueOfMethodLookup.put(Boolean.class, Boolean.class.getMethod("valueOf", new Class[]{String.class}));
            valueOfMethodLookup.put(Byte.class, Byte.class.getMethod("valueOf", new Class[]{String.class}));
            valueOfMethodLookup.put(Short.class, Short.class.getMethod("valueOf", new Class[]{String.class}));
            valueOfMethodLookup.put(Integer.class, Integer.class.getMethod("valueOf", String.class));
            valueOfMethodLookup.put(Float.class, Float.class.getMethod("valueOf", String.class));
            valueOfMethodLookup.put(Long.class, Long.class.getMethod("valueOf", String.class));
            valueOfMethodLookup.put(Double.class, Double.class.getMethod("valueOf", String.class));

            primitiveArrayGetMethodLookup.put(boolean.class, Array.class.getMethod("getBoolean", new Class[]{Object.class, int.class}));
            primitiveArrayGetMethodLookup.put(byte.class, Array.class.getMethod("getByte", new Class[]{Object.class, int.class}));
            primitiveArrayGetMethodLookup.put(short.class, Array.class.getMethod("getShort", new Class[]{Object.class, int.class}));
            primitiveArrayGetMethodLookup.put(int.class, Array.class.getMethod("getInt", Object.class, int.class));
            primitiveArrayGetMethodLookup.put(float.class, Array.class.getMethod("getFloat", Object.class, int.class));
            primitiveArrayGetMethodLookup.put(long.class, Array.class.getMethod("getLong", Object.class, int.class));
            primitiveArrayGetMethodLookup.put(double.class, Array.class.getMethod("getDouble", Object.class, int.class));
        } catch (NoSuchMethodException e) {

            //******************************
            // This can never happen
            //******************************
        }

    }

    public static boolean isArrayOfPrimitives(Object object) {

        if (object.getClass().isArray()) {
            return object.getClass().getComponentType().isPrimitive();
        }

        return false;
    }

    public static boolean isArrayOf(Object object, Class clazz) {

        if (object.getClass().isArray()) {
            return clazz.isAssignableFrom(object.getClass().getComponentType());
        }

        return false;
    }

    /**
     * Convert any array of primitives(excluding char), strings or numbers into any other array
     * of strings or numbers.
     *
     * @param array                       Array of primitives(excluding char), strings or numbers
     * @param convertedArrayComponentType Converted array component type (String or Number)
     * @param <T>                         To allow implicit casting
     * @return Array of convertedArrayComponentType
     */
    public static <T> T[] convertArray(Object array, Class<T> convertedArrayComponentType) {

        // Collect data regarding arguments
        final boolean arrayOfPrimitives = isArrayOfPrimitives(array);
        final boolean arrayOfCharPrimitives = isArrayOf(array, char.class);
        final boolean arrayOfCharacters = isArrayOf(array, Character.class);
        final boolean arrayOfStrings = isArrayOf(array, String.class);
        final boolean arrayOfNumbers = isArrayOf(array, Number.class);

        // Check if array is an array of strings, primitives or wrapped primitives
        if (!arrayOfPrimitives && !arrayOfNumbers && !arrayOfStrings || arrayOfCharPrimitives || arrayOfCharacters) {
            throw new IllegalArgumentException(array + " must be an array of of strings, primitives or boxed primitives (byte, boolean, short, int, float, long, double)");
        }

        // Check if it's assignable from Number of String
        if (!Number.class.isAssignableFrom(convertedArrayComponentType) && !String.class.isAssignableFrom(convertedArrayComponentType)) {
            throw new IllegalArgumentException(convertedArrayComponentType + " must be a Number or a String");
        }

        try {
            return (T[]) convertArrayInternal(array, convertedArrayComponentType);
        } catch (InvocationTargetException e) {

            // This can happen due to errors in conversion
            throw (RuntimeException) e.getTargetException();
        } catch (Exception e) {

            // This should never happen
            log.error("Something went really wrong in ArrayUtils.convertArray method.", e);
        }

        // To satisfy the compiler
        return null;
    }

    /**
     * Convert any array of primitives(excluding char), strings or numbers into an array
     * of primitives(excluding char).
     *
     * @param array                       Array of primitives(excluding char), strings or numbers
     * @param convertedArrayComponentType Converted array component type primitive(excluding char)
     * @return Array of convertedArrayComponentType
     */
    public static Object convertToPrimitiveArray(Object array, Class convertedArrayComponentType) {

        // Collect data regarding arguments
        final boolean arrayOfPrimitives = isArrayOfPrimitives(array);
        final boolean arrayOfCharPrimitives = isArrayOf(array, char.class);
        final boolean arrayOfCharacters = isArrayOf(array, Character.class);
        final boolean arrayOfStrings = isArrayOf(array, String.class);
        final boolean arrayOfNumbers = isArrayOf(array, Number.class);

        // Check if array is an array of strings, primitives or wrapped primitives
        if (!arrayOfPrimitives && !arrayOfNumbers && !arrayOfStrings || arrayOfCharPrimitives || arrayOfCharacters) {
            throw new IllegalArgumentException(array + " must be an array of of strings, primitives or boxed primitives (byte, boolean, short, int, float, long, double)");
        }

        // Check if it's assignable from Number of String
        if (!convertedArrayComponentType.isPrimitive() || convertedArrayComponentType.isAssignableFrom(char.class)) {
            throw new IllegalArgumentException(convertedArrayComponentType + " must be a primitive(excluding char)");
        }

        try {
            return convertArrayInternal(array, convertedArrayComponentType);
        } catch (InvocationTargetException e) {

            // This can happen due to errors in conversion
            throw (RuntimeException) e.getTargetException();
        } catch (Exception e) {

            // This should never happen
            log.error("Something went really wrong in ArrayUtils.convertArray method.", e);
        }

        // To satisfy the compiler
        return null;
    }

    private static Object convertArrayInternal(Object array, Class convertedArrayComponentType) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        // Lookup the primitive parse method or the boxed primitive valueOf method
        final Method convertMethod;
        if (convertedArrayComponentType.isPrimitive()) {
            convertMethod = primitiveParseMethodLookup.get(convertedArrayComponentType);
        } else {
            convertMethod = valueOfMethodLookup.get(convertedArrayComponentType);
        }

        // If the array is an array of primitives lookup the get method
        final Method primitiveArrayGetMethod = primitiveArrayGetMethodLookup.get(array.getClass().getComponentType());

        // Get length and create new array
        final int arrayLength = Array.getLength(array);
        final Object castedArray = Array.newInstance(convertedArrayComponentType, arrayLength);

        for (int i = 0; i < arrayLength; i++) {

            final Object value;
            if (primitiveArrayGetMethod != null) {
                value = primitiveArrayGetMethod.invoke(null, array, i);
            } else {
                value = Array.get(array, i);
            }

            final String stringValue = String.valueOf(value);
            final Object castedValue = convertMethod.invoke(null, stringValue);
            Array.set(castedArray, i, castedValue);
        }

        return castedArray;
    }

}

它可以这样使用:

String[] strings = new String[]{"1", "2", "3"};
Integer[] integers = ArrayUtils.convertArray(strings, Integer.class);
int[] ints = (int[]) ArrayUtils.convertToPrimitiveArray(strings, int.class);

I created this class for my own personal use but i think it can help you with your problem.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class ArrayUtils {

    private static final Logger log = LoggerFactory.getLogger(ArrayUtils.class);
    private static final Map<Class, Class> primitiveMapping = new HashMap<Class, Class>();
    private static final Map<Class, Method> primitiveParseMethodLookup = new HashMap<Class, Method>();
    private static final Map<Class, Method> primitiveArrayGetMethodLookup = new HashMap<Class, Method>();
    private static final Map<Class, Method> valueOfMethodLookup = new HashMap<Class, Method>();

    static {

        // Initialize primitive mappings
        primitiveMapping.put(boolean.class, Boolean.class);
        primitiveMapping.put(byte.class, Byte.class);
        primitiveMapping.put(short.class, Short.class);
        primitiveMapping.put(int.class, Integer.class);
        primitiveMapping.put(float.class, Float.class);
        primitiveMapping.put(long.class, Long.class);
        primitiveMapping.put(double.class, Double.class);

        // Initialize parse, valueOf and get method lookup
        // We do that in advance because the lookup of the method takes the longest time
        // Compared to the normal method call it's 20x higher
        // So we use just the reflective method call which takes double the time of a normal method call
        try {
            primitiveParseMethodLookup.put(boolean.class, Boolean.class.getMethod("parseBoolean", new Class[]{String.class}));
            primitiveParseMethodLookup.put(byte.class, Byte.class.getMethod("parseByte", new Class[]{String.class}));
            primitiveParseMethodLookup.put(short.class, Short.class.getMethod("parseShort", new Class[]{String.class}));
            primitiveParseMethodLookup.put(int.class, Integer.class.getMethod("parseInt", String.class));
            primitiveParseMethodLookup.put(float.class, Float.class.getMethod("parseFloat", String.class));
            primitiveParseMethodLookup.put(long.class, Long.class.getMethod("parseLong", String.class));
            primitiveParseMethodLookup.put(double.class, Double.class.getMethod("parseDouble", String.class));

            valueOfMethodLookup.put(Boolean.class, Boolean.class.getMethod("valueOf", new Class[]{String.class}));
            valueOfMethodLookup.put(Byte.class, Byte.class.getMethod("valueOf", new Class[]{String.class}));
            valueOfMethodLookup.put(Short.class, Short.class.getMethod("valueOf", new Class[]{String.class}));
            valueOfMethodLookup.put(Integer.class, Integer.class.getMethod("valueOf", String.class));
            valueOfMethodLookup.put(Float.class, Float.class.getMethod("valueOf", String.class));
            valueOfMethodLookup.put(Long.class, Long.class.getMethod("valueOf", String.class));
            valueOfMethodLookup.put(Double.class, Double.class.getMethod("valueOf", String.class));

            primitiveArrayGetMethodLookup.put(boolean.class, Array.class.getMethod("getBoolean", new Class[]{Object.class, int.class}));
            primitiveArrayGetMethodLookup.put(byte.class, Array.class.getMethod("getByte", new Class[]{Object.class, int.class}));
            primitiveArrayGetMethodLookup.put(short.class, Array.class.getMethod("getShort", new Class[]{Object.class, int.class}));
            primitiveArrayGetMethodLookup.put(int.class, Array.class.getMethod("getInt", Object.class, int.class));
            primitiveArrayGetMethodLookup.put(float.class, Array.class.getMethod("getFloat", Object.class, int.class));
            primitiveArrayGetMethodLookup.put(long.class, Array.class.getMethod("getLong", Object.class, int.class));
            primitiveArrayGetMethodLookup.put(double.class, Array.class.getMethod("getDouble", Object.class, int.class));
        } catch (NoSuchMethodException e) {

            //******************************
            // This can never happen
            //******************************
        }

    }

    public static boolean isArrayOfPrimitives(Object object) {

        if (object.getClass().isArray()) {
            return object.getClass().getComponentType().isPrimitive();
        }

        return false;
    }

    public static boolean isArrayOf(Object object, Class clazz) {

        if (object.getClass().isArray()) {
            return clazz.isAssignableFrom(object.getClass().getComponentType());
        }

        return false;
    }

    /**
     * Convert any array of primitives(excluding char), strings or numbers into any other array
     * of strings or numbers.
     *
     * @param array                       Array of primitives(excluding char), strings or numbers
     * @param convertedArrayComponentType Converted array component type (String or Number)
     * @param <T>                         To allow implicit casting
     * @return Array of convertedArrayComponentType
     */
    public static <T> T[] convertArray(Object array, Class<T> convertedArrayComponentType) {

        // Collect data regarding arguments
        final boolean arrayOfPrimitives = isArrayOfPrimitives(array);
        final boolean arrayOfCharPrimitives = isArrayOf(array, char.class);
        final boolean arrayOfCharacters = isArrayOf(array, Character.class);
        final boolean arrayOfStrings = isArrayOf(array, String.class);
        final boolean arrayOfNumbers = isArrayOf(array, Number.class);

        // Check if array is an array of strings, primitives or wrapped primitives
        if (!arrayOfPrimitives && !arrayOfNumbers && !arrayOfStrings || arrayOfCharPrimitives || arrayOfCharacters) {
            throw new IllegalArgumentException(array + " must be an array of of strings, primitives or boxed primitives (byte, boolean, short, int, float, long, double)");
        }

        // Check if it's assignable from Number of String
        if (!Number.class.isAssignableFrom(convertedArrayComponentType) && !String.class.isAssignableFrom(convertedArrayComponentType)) {
            throw new IllegalArgumentException(convertedArrayComponentType + " must be a Number or a String");
        }

        try {
            return (T[]) convertArrayInternal(array, convertedArrayComponentType);
        } catch (InvocationTargetException e) {

            // This can happen due to errors in conversion
            throw (RuntimeException) e.getTargetException();
        } catch (Exception e) {

            // This should never happen
            log.error("Something went really wrong in ArrayUtils.convertArray method.", e);
        }

        // To satisfy the compiler
        return null;
    }

    /**
     * Convert any array of primitives(excluding char), strings or numbers into an array
     * of primitives(excluding char).
     *
     * @param array                       Array of primitives(excluding char), strings or numbers
     * @param convertedArrayComponentType Converted array component type primitive(excluding char)
     * @return Array of convertedArrayComponentType
     */
    public static Object convertToPrimitiveArray(Object array, Class convertedArrayComponentType) {

        // Collect data regarding arguments
        final boolean arrayOfPrimitives = isArrayOfPrimitives(array);
        final boolean arrayOfCharPrimitives = isArrayOf(array, char.class);
        final boolean arrayOfCharacters = isArrayOf(array, Character.class);
        final boolean arrayOfStrings = isArrayOf(array, String.class);
        final boolean arrayOfNumbers = isArrayOf(array, Number.class);

        // Check if array is an array of strings, primitives or wrapped primitives
        if (!arrayOfPrimitives && !arrayOfNumbers && !arrayOfStrings || arrayOfCharPrimitives || arrayOfCharacters) {
            throw new IllegalArgumentException(array + " must be an array of of strings, primitives or boxed primitives (byte, boolean, short, int, float, long, double)");
        }

        // Check if it's assignable from Number of String
        if (!convertedArrayComponentType.isPrimitive() || convertedArrayComponentType.isAssignableFrom(char.class)) {
            throw new IllegalArgumentException(convertedArrayComponentType + " must be a primitive(excluding char)");
        }

        try {
            return convertArrayInternal(array, convertedArrayComponentType);
        } catch (InvocationTargetException e) {

            // This can happen due to errors in conversion
            throw (RuntimeException) e.getTargetException();
        } catch (Exception e) {

            // This should never happen
            log.error("Something went really wrong in ArrayUtils.convertArray method.", e);
        }

        // To satisfy the compiler
        return null;
    }

    private static Object convertArrayInternal(Object array, Class convertedArrayComponentType) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        // Lookup the primitive parse method or the boxed primitive valueOf method
        final Method convertMethod;
        if (convertedArrayComponentType.isPrimitive()) {
            convertMethod = primitiveParseMethodLookup.get(convertedArrayComponentType);
        } else {
            convertMethod = valueOfMethodLookup.get(convertedArrayComponentType);
        }

        // If the array is an array of primitives lookup the get method
        final Method primitiveArrayGetMethod = primitiveArrayGetMethodLookup.get(array.getClass().getComponentType());

        // Get length and create new array
        final int arrayLength = Array.getLength(array);
        final Object castedArray = Array.newInstance(convertedArrayComponentType, arrayLength);

        for (int i = 0; i < arrayLength; i++) {

            final Object value;
            if (primitiveArrayGetMethod != null) {
                value = primitiveArrayGetMethod.invoke(null, array, i);
            } else {
                value = Array.get(array, i);
            }

            final String stringValue = String.valueOf(value);
            final Object castedValue = convertMethod.invoke(null, stringValue);
            Array.set(castedArray, i, castedValue);
        }

        return castedArray;
    }

}

It can be used like this:

String[] strings = new String[]{"1", "2", "3"};
Integer[] integers = ArrayUtils.convertArray(strings, Integer.class);
int[] ints = (int[]) ArrayUtils.convertToPrimitiveArray(strings, int.class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文