2016-09-07 1 views
-5

의리스트를 처리 ArrayList에의 stracture은 : 주먹 새로운 배열제가 N 어레이를 추출하려는 arrayliste에서 자바

  • 번호 제 배열
  • 크기
  • 소자 두번째 배열
  • 소자의 제 2 어레이
  • 배열 크기
  • ,745,151 ...

아무쪼록 저를 도와주세요. 어떻게 새로운 N 배열에서이 수를 추출 할 수 있습니까?

EXP

50  new array's number 
3   (size of tab1= 3) 
2157  first element of the first array ==> tab1[1]= 2157 
158  second element of the first array==> tab1[2]= 2158 
2172  third element of the first array==> tab1[3]=2172 
12  The second array has 12 samples (size of tab2=12) 
2360  first element of the second array ==> tab2[1]= 2360 
2367  second element of the second array ==> tab2[2]= 2367 
2375 
2375 
+1

이미 시도한 바가 있습니까? –

+0

자신 만의 노력을 보여 주어야합니다. 그래서 무료 코드 서비스가 아니기 때문에 사람들은 서로의 도움을 받아야합니다. – YakovL

+0

제안 (코드)을 게시했습니다. 내 의견을 확인하고 알려주십시오. –

답변

0

나는 새로운 arrayliste을 만들고 난 exemple 시도보다

ArrayList<Integer> al= new ArrayList<>(); 
int a0= al.get(0); 
a1= al.get(1); 
int a2=al.get(a1+ 2); 
int a3= al.get(a2+a1+3); 
int a4=al.get(a3+a2+a1+4); 
int a5=al.get(a4+a3+a2+a1+5); 

내가 코드를 보는 것이 생성하는 새로운 배열의 크기를 추출

static String fact (String n1) 
    { 
      // if (n==0) return 1; 
     int n= Integer.parseInt(n1); 
     String han=""+n; 

     for (int k=1;k<n;k++){ 
     han=han+"+ a"+(n-k); 


     } 

     return han; 
    } 

문자열을 구문 분석 할 때 작동하지 않습니다.

for (int h=2;h<al.get(0);h++){ 

       int han=fact(""+h); 
       int hane= Integer.parseInt(han);} 

이 오류

Exception in thread "main" java.lang.NumberFormatException: For input string: "2+ a1" 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
+0

'al.get (0);'가 잘못되었습니다. 빈 배열 목록을 만든 직후. for 루프에서'fact()'호출은 문자열''2+ a1 ''을 반환합니다. 문자열에는 정수 만 포함되어 있지 않으므로 정수로 구문 분석하려는 시도는 예외 및 스택 추적과 함께 실패합니다. 추신 : 질문을 편집하고 추가 정보를 추가 할 수 있습니다. 답변으로 제공 할 필요는 없습니다.PPS'fact()'를 호출하기 전에 정수를 문자열로 변환하고,'fact()'가 그것을 다시 변환하는 것이지, 그렇게 중복되지 않습니까? –

+0

arraylist를 생성하려고 시도 했습니까? 아니면 arraylist에서 무언가를 추출하려고 시도 했습니까? 나는 당신이하고 싶었던 것과 코드가하는 것 사이의 일치를 볼 수 없기 때문에 아무것도 제안하기가 정말로 어렵습니다. –

+0

내 아이디어는 모든 새로운 배열 끝의 크기를 추출한 다음 각 배열에 요소를 추가하는 것입니다. 그래서 문제가 생겼고 다른 아이디어로 다른 코드를 작성해야합니다. 제발 도와 주실 수 있습니까? –

0

먼저 되 잖아는, 나는 당신이 뭘 하려는지의 많은 부분을 이해하지 않았습니다. 두 번째로 그리고 더 심각하게, 저는 여러분에게이 많은 코드를 작성하여 여러분에게 호의적으로 또는 반대를하고 있는지 의심 스럽습니다. 당신은 아마 당신 자신을 써서 더 많은 것을 배울 것입니다. 어쨌든, 당신은 그것을 가져 가거나 무시할 수 있습니다.

나는 목록에서 정보를 추출에서 목록의 생성을 분리 좋아, 그래서 두 개의 서로 다른 메소드를 호출하고 싶습니다 :

ArrayList<ArrayList<Integer>> listOfArrayLists = generateLists(); 
    extractInfo(listOfArrayLists); 

(평소 스타일은 첫 번째 메서드 호출 및 방법을 작성하는 것입니다 나중에 다른 방향으로 선호 할 수 있습니다).

private static ArrayList<ArrayList<Integer>> generateLists() { 
    ArrayList<ArrayList<Integer>> result = new ArrayList<>(); 

    ArrayList<Integer> newArrayList = new ArrayList<>(); 
    newArrayList.add(2157); 
    newArrayList.add(158); 
    newArrayList.add(2172); 
    result.add(newArrayList); 

    newArrayList = new ArrayList<>(); 
    newArrayList.add(2360); 
    newArrayList.add(2357); 
    newArrayList.add(2375); 
    result.add(newArrayList); 

    newArrayList = new ArrayList<>(); 
    newArrayList.add(725); 
    result.add(newArrayList); 

    return result; 
} 

private static void extractInfo(ArrayList<ArrayList<Integer>> listOfArrayLists) { 
    System.out.println("Number of array lists: " + listOfArrayLists.size()); 
    for (ArrayList<Integer> al : listOfArrayLists) { 
     System.out.println("Size of list: " + al.size()); 
     System.out.println("Elements:"); 
     for (Integer i : al) { 
      System.out.println(i); 
     } 
    } 
} 

코드 인쇄 :

Number of array lists: 3 
Size of list: 3 
Elements: 
2157 
158 
2172 
Size of list: 3 
Elements: 
2360 
2357 
2375 
Size of list: 1 
Elements: 
725 

난 당신이 코드를 이해하고 그것에서 배울 수 있습니다 희망 여기에 방법이 있습니다. 나는 이것이 당신의 야망이라면 - 언젠가 당신은 당신이 호의를 돌려주고 나를위한 코드를 작성할 수있을만큼 좋을 것입니다. :-)

관련 문제