在Java中将Object []数组转换为int []数组?

前端之家收集整理的这篇文章主要介绍了在Java中将Object []数组转换为int []数组?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
似乎没有简单的方法可以做到这一点,但这是我到目前为止所做的,如果有人能够纠正它,使其工作将是伟大的.在“newarray [e] = array [i] .intValue();”我收到一个错误“没有命名方法”intValue“在类型” java.lang.Object“中找到.”
救命!
/*
Description: A game that displays digits 0-9 and asks the user for a number N.
 It then reverses the first N numbers of the sequence. It continues this until
 all of the numbers are in order.
 numbers

*/

import hsa.Console;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;


public class ReversalGame3test

{
    static Console c;

    public static void main (String[] args)
{
    c = new Console ();

    c.println ("3. REVERSAL GAME");
    c.println ("");
    c.println ("Displayed below are the digits 0-9 in random order. You must then enter a");
    c.println ("number N after which the computer will reverse the first N numbers in the");
    c.println ("sequence. The goal of this game is to sort all of the numbers in the fewest");
    c.println ("number of reversals.");
    c.println (""); //introduction

    List numbers = new ArrayList ();
    numbers.add ("0");
    numbers.add ("1");
    numbers.add ("2");
    numbers.add ("3");
    numbers.add ("4");
    numbers.add ("5");
    numbers.add ("6");
    numbers.add ("7");
    numbers.add ("8");
    numbers.add ("9");
    Collections.shuffle (numbers);
    Object[] array = numbers.toArray (new String [10]); // declares + shuffles numbers and converts them to array

    c.print ("Random Order: ");
    for (int i = 0 ; i < 10 ; i++)
    {
        c.print ((array [i]) + " ");
    }
    c.println ("");

    boolean check = false;
    boolean check2 = false;
    String NS;
    int N = 0;
    int count = 0;
    int e = -1;
    int[] newarray = new int [10];

    //INPUT
    do
    {
        c.print ("Enter a number: ");
        NS = c.readString ();
        count += 1;

        check = isInteger (NS);
        if (check == true)
        {
            N = Integer.parseInt (NS);
            if (N < 1 || N > 10)
            {
                check = false;
                c.println ("ERROR - INPUT NOT VALID");
                c.println ("");
            }
            else
            {
                c.print ("Next Order: ");
                for (int i = N - 1 ; i > -1 ; i--)
                {
                    e += 1;
                    newarray [e] = array [i].intValue ();
                    c.print ((newarray [e]) + " ");
                }
                for (int i = N ; i < 10 ; i++)
                {
                    e += 1;
                    newarray [e] = array [i].intValue ();
                    c.print ((newarray [e]) + " ");
                }
                check2 = sorted (newarray);
            } // rearranges numbers if valid
        } // checks if N is valid number
    }
    while (check == false);
} // main method


public static boolean isInteger (String input)
{
    try
    {
        Integer.parseInt (input);
        return true;
    }
    catch (NumberFormatException nfe)
    {
        return false;
    }
} //isInteger method


public static boolean sorted (int array[])
{
    boolean isSorted = false;

    for (int i = 0 ; i < 10 ; i++)
    {
        if (array [i] < array [i + 1])
        {
            isSorted = true;
        }
        else if (array [i] > array [i + 1])
        {
            isSorted = true;
        }
        else
            isSorted = false;

        if (isSorted != true)
            return isSorted;
    }
    return isSorted;
} // sorted method

}

解决方法

你可以使用 Integer.valueOf.
Integer.valueOf((String) array [i])

Integer类有一个方法valueOf,它以字符串作为值并返回一个int值,你可以使用它.如果传递给它的字符串不是有效的整数值,它将抛出NumberFormatException.

此外,如果您使用的是java5或更高版本,则可以尝试使用generics来使代码更具可读性.

原文链接:https://www.f2er.com/java/128261.html

猜你在找的Java相关文章