Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

dart: access function from list

Edit: i know, always call the first element on list, it isnt the point. i want to call numbers[0] func. and it regenerate new int.actually codes are not same which mine, i have a custom class which based on functions with random int and i need to use list of my custom class , so if i use func in list it will be awesome, how can i make new numbers list each time. when app start list regenerated, but i want when i call the list, it will regenerated

i want to print new int for each print but it prints same int , i tried so many thing and i cant figure out

void main{
  int ramdomint(){
    
   final _random = new Random();
    int _num =  _random.nextInt(100);
    return _num;
  }
  
  List<int> numbers=[ramdomint(),ramdomint(),ramdomint()];
  
  void printNums(){
    for(var i=0;i<3;i++){
      List<int> newNumbers =new List.from(numbers); //what can i use for this?
      print(newNumbers[0]); //edit:i dont want [i], iwant to use ewNumbers[0] for new int for each time
    }
  }
  printNums();
// expected new int for each but same one
}

solution from a friend:

import 'dart:math';

int get ramdomint => Random().nextInt(100);

List<int> get numbers => [ramdomint, ramdomint, ramdomint];

void main() {
  for (var i = 0; i < 3; i++) {
    print(numbers[0]);
  }
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Do not nest functions. Move ramdomint and printNums outside main function.
  2. Add an empty list of arguments to the main function.
  3. printNums: pass list of numbers as an argument.
  4. printNums: you don't need to copy the list to the newNumbers if you want only to display the content of the list.
  5. printNums: the problem is, you access only first element of the list (with 0 index).
import 'dart:math';

void main() {
  List<int> numbers = [ramdomint(), ramdomint(), ramdomint()];

  printNums(numbers);
}

int ramdomint() => Random().nextInt(100);

void printNums(List<int> numbers) {
  // Easier way:
  for (int item in numbers) {
    print(item);
  }

  // Your way:
  for (int i = 0; i < numbers.length; i++) {
    print(numbers[i]);
  }
}

EDIT:

According to @jamesdlin's comment, you can extend list class to randomize unique values in the list:

import 'dart:math';

void main() {
  var numbers = <int>[]..randomize();

  printNums(numbers);
}

void printNums(List<int> numbers) {
  // Easier way:
  for (int item in numbers) {
    print(item);
  }

  // Your way:
  for (int i = 0; i < numbers.length; i++) {
    print(numbers[i]);
  }
}

extension on List<int> {
  void randomize({
    int length = 3,
    int maxValue = 100,
  }) {
    final generator = Random();
    for (var i = 0; i < length; i++) {
      add(generator.nextInt(maxValue));
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...