0

さて、私はこのコードを持っていて、整数の配列の値をtwholef [x]に保存し、それらの値を配列wholelist [y]に保存したいと思います。唯一の問題は、私が設定した方法は、配列の最初の 3 つの値を取り、それらを intwholef[x] に格納し、次の行を通過するときに x がリセットされることです。グラフィカルな表現は以下のとおりです

これは、文字列の配列に格納されたファイルの内容です

intwholef[0] = 1 3 10
intwholef[1] = 2 4 15
intwholef[2] = 3 6 8
intwholef[3] = 4 7 3
intwholef[4] = 5 9 12

今私が欲しいのは、これらの値をこのように保存することです。

wholelist[] = 1,3,10,2,4,15,3,6,8,4,7,3,5,9,12

のようにアクセスできます

wholelist[2] * wholelist[5] = 150;

私が直面している問題は、このようなリストに値を保存できないことです。何かアイデアはありますか?

これがコード全体です。私が話している部分は一番下にあります

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class project1
{
    @SuppressWarnings("null")
    public static void main(String[] args)
    {
        System.out.println("These are your following choices: ");
        System.out.println("1. First-Come First-Served (FCFS): ");
        System.out.println("2. Shortest Job Next (SJN): ");
        System.out.println("3. Shortest Remaining Time (SRT): ");
        System.out.println("4. Round Robin (RR) with time quantum = 4 ms: ");
        System.out.println("please enter your choice by entering 1, 2, 3, 4");
        Scanner in = new Scanner(System.in);
        int choice = in.nextInt();
        if(choice ==1)
        {
            System.out.println("your choice was first come first serve");
        }
        else if(choice == 2)
        {
            System.out.println("your choice was shortest job next");
        }
        else if(choice == 3)
        {
            System.out.println("your choice was shortest job remaining");
        }
        else if(choice == 4)
        {
            System.out.println("your choice was round robin (rr) with time quantum = 4 ms");
        }
        else
        {
            System.out.println("you entered an invalid choice");
        }
        BufferedReader file = null;

        System.out.println("Please enter the file path for your input");
        Scanner fp = new Scanner(System.in);
        String input = fp.nextLine();
        String fileloc;
        String[] wholef = null;
        int fline = 0;
        try 
        {
            file = new BufferedReader(new FileReader(input));
            fileloc = file.readLine();
            fline = Integer.parseInt(fileloc);  // this stands for file contents from the file to be read
            wholef = new String[fline];
            int i = 0;
            while((fileloc = file.readLine()) != null)
            {
                wholef[i] = fileloc;
                System.out.print("the contents of this file are: ");
                System.out.println(fileloc);
                i++;
            }
        System.out.println("This is the contents of the file stored in an array of strings");
        for(int n = 0; n < fline; n++)
        {
            System.out.println(wholef[n]);
        }
        } catch (IOException er)
        {
            er.printStackTrace();
        }
        finally
        {
        try
        {
            if(file != null)
            {
                file.close();
            }
        } catch(IOException erx)
        {
            erx.printStackTrace();
        }
        }

        System.out.println("This is the size of the contents of the file");
        for(int n = 0; n < fline; n++)
        {
            System.out.println(wholef[n].length());
        }

        System.out.println("this is the input file converted and stored into an array of integers");

        String[] parts = null;
        int[] intwholef = null;
        int[] wholelist =null;
        for(int x = 0; x < fline; x++)
        {
            parts = wholef[x].split(" ");

            intwholef= new int[parts.length];

            for(int n = 0; n < parts.length; n++)
            {
                intwholef[n] = Integer.parseInt(parts[n]);
                System.out.println(/*"intwholef[" + n + "] = " + */intwholef[n]);
                for(int m = 0; m < parts.length; m++)
                {
                    //wholelist[m]= intwholef[n];
                }
            }
        }

        System.out.println("this is the list of number from the conversion dumped into a singular array list");

        for(int i = 0; i < 15; i++)
        {
            System.out.println("intwholef[" + i + "] = " + wholelist[i]);
        }
        /*
        System.out.println("operations done with array of ints");
        for(int i = 0; i < 20; i++)
        {
            System.out.println(intwholef[i]);
        }
        //System.out.println(intwholef[0] * intwholef[3]);
        */

    }
}
4

2 に答える 2

2

サイズは変更される可能性があるため、配列の代わりにリスト(ArrayListなど)を使用することをお勧めします。そのような:

ArrayList wholeList = new ArrayList<Integer>();
for(int x = 0; x < fline; x++)
    {
        parts = wholef[x].split(" ");
        for(int n = 0; n < parts.length; n++)
        {
            wholeList.add(Integer.parseInt(parts[n]));
        }
    }

次に、を使用してさまざまな値にアクセスできますwholeList.get(index)

于 2013-03-07T23:18:48.597 に答える
0

ArrayList を使用して簡単に実行できます。最終的に配列にしたい場合は、toStringメソッドを使用してください。文字列を で分割して\\s、空白を区切り文字として使用できるようにします。また、次のように、ループのみが必要です。

    int[] myArray = new int[10];
    List<Integer> list = new ArrayList<Integer>();
    while ((fileloc = file.readLine()) != null) {
        for (String s : fileloc.split("\\s+")) {
            list.add(Integer.parseInt(s));
        }
        System.out.println(Arrays.toString(myArray));
    }
    int[] wholelist = list.toArray();
于 2013-03-07T23:21:57.720 に答える