2013-03-07 33 views
0

好吧,我有這段代碼,我想將整數數組的值存儲在twholef [x]中,並將這些值存儲在數組wholelist [ Y]。唯一的問題是,我設置的方式是,取數組的前三個值並將它們存儲在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; 

,即時通訊運行到的是,即時通訊無法保存在這樣的列表中的值,任何想法的問題訪問?

這裏是整個代碼,部分IM談論的是在底部

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]); 
     */ 

    } 
} 

回答

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)訪問不同的值。

+0

好這個工作,但是當我試圖把它們打印出來,它給了我一個錯誤,使用的System.out.println(wholelist.get(I)) ;在for循環中遞增變量i。如果那不是你如何訪問他們,那麼我將如何去做呢? – user1721540 2013-03-07 23:34:09

+0

沒關係,我看到我在另一個位置發生錯誤,再次感謝您的幫助! – user1721540 2013-03-07 23:37:04

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(); 
相關問題